簡體   English   中英

如何使用R中列表列中的名稱重命名文件夾中的文件列表

[英]How to rename a list of files in a folder with names in a list column in R

我正在嘗試在文件夾中使用.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)

文件夾中的文件具有通用名稱: S01_S1.txtS02_S2.txt等。我還有一個文件,其中包含一個包含2列的表。 第一列通過前三個字符標識每個文件,例如S05,S06,... S45。 第二列具有該行中文件的相應新名稱,例如RK_ci1151_01,RK_ci1151_02,... RK_ci1151_Baseline。 我正在嘗試重命名文件,以便將名稱更改為RK_ci1151_01.txt,RK_ci1151_02 ..等等。

我也得到了

Error in file.rename(tcr.sample, pattern=".txt", replacement=new.names) : unused arguments (pattern = ".txt, replacement=new.names)

信息。

我認為您可以使用其他方法來實現文件重命名。 如果CSV文件列出了所需的唯一文件名,並且它們與唯一的“分組”變量關聯(在您的情況下,“ S01”與文件RK_ci1151_01,RK_ci1151_02,RK_ci1151_Baseline關聯),則可以使用新名稱來重新創建舊名稱。 換句話說,您可以用舊文件名的模式替換新文件名中“ _01.txt”,“ _ 02.txt”等之前的模式。 然后將數據file.rename的列用作file.rename調用中的from=to=參數。

### prep toy data
# create df with old and new names
df <- data.frame(old=paste0(rep(letters[1:3],each=3),
                            '_', rep(c(0:2),3), '.txt'),
                 new=paste0(rep(c('foo','bar','hello'),each=3),
                            '_', rep(c(0:2),3), '.txt'),
                 stringsAsFactors = F)

# write files with old names
for (i in 1:length(df$old)) {
  write.table(NULL,df$old[i])
}

list.files(pattern='\\.txt')

[1] "a_0.txt" "a_1.txt" "a_2.txt" "b_0.txt" "b_1.txt" "b_2.txt" "c_0.txt" "c_1.txt" "c_2.txt"

# edit old names to match user code
df$old <- sub('_[0-9]\\.txt','',df$old)

> df
  old         new
1   a   foo_0.txt
2   a   foo_1.txt
3   a   foo_2.txt
4   b   bar_0.txt
5   b   bar_1.txt
6   b   bar_2.txt
7   c hello_0.txt
8   c hello_1.txt
9   c hello_2.txt

# separate new file names to join with old
df$join <- sub('.*(_[0-9]\\.txt)','\\1',df$new)
df$old1 <- paste0(df$old,df$join)

# rename
file.rename(df$old1, df$new)
list.files(pattern='\\.txt')

[1] "bar_0.txt"   "bar_1.txt"   "bar_2.txt"   "foo_0.txt"   "foo_1.txt"   "foo_2.txt"  
[7] "hello_0.txt" "hello_1.txt" "hello_2.txt"
# 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM