如何使用R中列表列中的名称重命名文件夹中的文件列表

如何使用R中列表列中的名称重命名文件夹中的文件列表,r,R,我正在尝试重命名文件夹中扩展名为.txt的文件,该文件夹在表的列中具有相应的名称列表。该表包含两个向量,第一个是文件夹中文件名的标题,第二个是我希望保留原始扩展名的实际名称。我可以使用文件重命名,但如何用相应行中的新名称替换它 我尝试过使用file.rename循环,只是代码会遍历表中每个文件夹的所有新名称。不确定是否有一个R函数可以实现这一点 library(dplyr) library(stringr) headers <- read.csv(file="/mnt/data/Deve

我正在尝试重命名文件夹中扩展名为.txt的文件,该文件夹在表的列中具有相应的名称列表。该表包含两个向量,第一个是文件夹中文件名的标题,第二个是我希望保留原始扩展名的实际名称。我可以使用文件重命名,但如何用相应行中的新名称替换它

我尝试过使用file.rename循环,只是代码会遍历表中每个文件夹的所有新名称。不确定是否有一个R函数可以实现这一点

library(dplyr)
library(stringr)

headers <- read.csv(file="/mnt/data/Development/Sequences/SampleSheet.csv", skip = 18, header = F, nrows =1, as.is = T)

sample.rows = read.csv(file="/mnt/data/Development/Sequences/SampleSheet.csv", skip = 19, header = F)

colnames(sample.rows) = headers

old.new.names <- select(sample.rows, Sample_Name, Description)

startingDir<-"/mnt/data/Development/Sequences"

tcr.sample <- list.files(path=startingDir, pattern="txt", full.names=TRUE )

new.names <- select(old.new.names, Description)

file.rename(list.files(tcr.sample, pattern = ".txt" replacement=new.names)

消息。

我认为您可以使用不同的方法实现文件重命名。如果您的CSV文件列出了所需的唯一文件名,并且它们与唯一的“分组”变量关联(在您的情况下,“S01”与文件RK_ci1151_01、RK_ci1151_02、RK_ci1151_Baseline关联),则可以使用新名称重新创建旧名称。换句话说,您可以用旧文件名的模式替换新文件名中“_01.txt”、“_02.txt”等之前的模式。然后使用数据帧的列作为
文件中的
from=
to=
参数。重命名
调用

###准备玩具数据
#使用新旧名称创建df
df
#用name替换标准Iseq100输出样本名称的脚本
#在SampleSheet.csv文件的“说明”列中。
图书馆(dplyr)
图书馆(stringr)
#将工作目录设置为示例文件(.txt)所在的文件夹。
setwd(“/mnt/data/Development/Sequences”)
#提取文件中示例表的标题。

这是一个错误,不是一条消息。这里的代码中有一些语法错误(例如,
pattern=“.txt”
replacement=
)之间没有逗号),因此不清楚问题是由这些问题还是其他原因造成的。您是否可以根据实际使用的内容进行更新?(您似乎缺少
sub
gsub
code?)pattern=和replacement=之间需要逗号吗?抱歉,原始代码中有一个逗号。我忘了在抄写信息时把它包括在内。下面是实际的行:file.rename(tcr.sample,pattern=“.txt”,replacement=new.names)。“new.names”实际上是一个变量,它包含一个按行列出所有新名称的向量。我怀疑我没有使用正确的模式,因为.txt实际上是我要保留的文件的唯一部分。我道歉。我是R的新手,我不确定file.rename实际上是如何工作的。kstew,你的建议并不像我希望的那样有效,但它为我指明了正确的方向。我在下面发布我的解决方案。谢谢。下次,在你的问题中加入一个min-reprex会很有帮助,
Error in file.rename(tcr.sample, pattern=".txt", replacement=new.names) : unused arguments (pattern = ".txt, replacement=new.names)
# Script to replace the standard Iseq100 output sample names with name 
# in the "Description" column in the SampleSheet.csv file.

library(dplyr)
library(stringr)

# Set the working directory to folder where sample files (.txt) are located.
setwd("/mnt/data/Development/Sequences")

# Extract the headers of the sample table in the file.
headers <- read.csv(file="SampleSheet.csv", skip = 18, header = F, nrows =1, as.is = T)

# Extract the sample rows.
sample.rows = read.csv(file="SampleSheet.csv", skip = 19, header = F)

# Add the headers to the sample rows
colnames(sample.rows) = headers

# Extract the "Descrription" column which contains the actual names of the sample
new.names <- select(sample.rows, Description)
new.names <- paste0(new.names$Description, '.txt')

# Extract target .txt files and rename to Description name.
old.names <- list.files(path=".", pattern=".txt")
file.rename(from=old.names, to=new.names)