R:使用数据框中特定列的元素重命名文件

问题描述 投票:0回答:1

我有一个.txt文件的文件夹,每个文件都有一个长字符串名,例如“ ctrl_Jack_DrugA_XXuM.txt”。但是,该名称缺少重要的字符串,即时间戳。

但是,我在每个文件的数据框中都有该信息。例如,在每个文件中,包含多个列,其中一个列称为“ Pid_treatmentsum”:其中的元素是“ Jack_R4_200514_DrugA_XXuM.txt”

因此,在我继续进行下游操作之前,我想根据诸如Jack和时间戳之类的名称(如“ R4_200514”)将文件分类为子文件夹,为此,我需要使用“ Pid_treatmentsum”重命名文件标题。

现在输入代码:

```
#create MRE
#file 1
Row <- c(rep("16", 20))
column <- c(rep("3", 20))
Pid<- c(rep("Jack", 20))
Stimulation<- c(rep("3S", 20))
Drug <- c(rep("2DG", 20))
Dose <-c(rep("3uM", 20))
Treatmentsum <-c(rep(paste("Jack","3S",'2DG','3uM',sep = "_"), 20))
PiD_treatmentsum <- c(rep(paste('Jack',"T4_20200501",'3S','2DG','3uM',sep = "_"), 20))
sampleset <-data.frame(Row,column,Pid,Stimulation,Drug,Dose,Treatmentsum,PiD_treatmentsum)
write.table(sampleset, file = "ctrl_Jack_3S_2DG_3uM.txt",sep="\t", row.names = F, col.names = T)

#file 2
Row <- c(rep("16", 40))
column <- c(rep("3", 40))
Pid<- c(rep("Mark", 40))
Stimulation<- c(rep("3S", 40))
Drug <- c(rep("STS", 40))
Dose <-c(rep("1uM", 40))
Treatmentsum <-c(rep(paste("Mark","3S",'STS','1uM',sep = "_"), 40))
PiD_treatmentsum <- c(rep(paste('Mark',"T5_20200501",'3S','STS','1uM',sep = "_"), 40))
sampleset <-data.frame(Row,column,Pid,Stimulation,Drug,Dose,Treatmentsum,PiD_treatmentsum)
write.table(sampleset, file = "ctrl_Mark_3S_STS_1uM.txt",sep="\t", row.names = F,col.names = T)

# rename all the files using their PiD_treatmentsum 
filenames <- list.files("C:/UsersXXX", pattern="*.txt")
outdirectory <- "~/out"
lapply(filenames, function(x) {
df <- read.csv(x,sep="\t", header=TRUE, fill = T,stringsAsFactors = F)
a <- as.character(unique(df[["PiD_treatmentsum"]]))
b<-paste0("ctrl_",a, '.txt', sep="")
newname <- file.rename(basename(x), b)
write.table(df, paste0(outdirectory,"/", newname, sep="\t", 
          quote=FALSE, row.names=F, col.names=TRUE)
})

此处显示意外错误}。我想我一定已经搞砸了。

[如果我仅分解代码并运行一个文件作为示例,则代码有效:

  df <- read.csv('ctrl_Jack_3S_2DG_3uM.txt',sep="\t", header=TRUE, 
             fill = T,stringsAsFactors=F)

  a <- as.character(unique(df[["PiD_treatmentsum"]]))
  b<-paste0("ctrl_",a, '.txt', sep="")
  basename('ctrl_Jack_3S_2DG_3uM.txt')
  file.rename(basename('ctrl_Jack_3S_2DG_3uM.txt'), b)

```

一些帮助和解释将不胜感激:)

r rename file-rename
1个回答
0
投票

这应该起作用:

create MRE
#file 1
Row <- c(rep("16", 20))
column <- c(rep("3", 20))
Pid<- c(rep("Jack", 20))
Stimulation<- c(rep("3S", 20))
Drug <- c(rep("2DG", 20))
Dose <-c(rep("3uM", 20))
Treatmentsum <-c(rep(paste("Jack","3S",'2DG','3uM',sep = "_"), 20))
PiD_treatmentsum <- c(rep(paste('Jack',"T4_20200501",'3S','2DG','3uM',sep = "_"), 20))
sampleset <-data.frame(Row,column,Pid,Stimulation,Drug,Dose,Treatmentsum,PiD_treatmentsum)
write.table(sampleset, file = "ctrl_Jack_3S_2DG_3uM.txt",sep="\t", row.names = F, col.names = T)

#file 2
Row <- c(rep("16", 40))
column <- c(rep("3", 40))
Pid<- c(rep("Mark", 40))
Stimulation<- c(rep("3S", 40))
Drug <- c(rep("STS", 40))
Dose <-c(rep("1uM", 40))
Treatmentsum <-c(rep(paste("Mark","3S",'STS','1uM',sep = "_"), 40))
PiD_treatmentsum <- c(rep(paste('Mark',"T5_20200501",'3S','STS','1uM',sep = "_"), 40))
sampleset <-data.frame(Row,column,Pid,Stimulation,Drug,Dose,Treatmentsum,PiD_treatmentsum)
write.table(sampleset, file = "ctrl_Mark_3S_STS_1uM.txt",sep="\t", row.names = F,col.names = T)

我只更改了最后三行。我们使用file.rename重命名文件(newname现在为TRUEFALSE,如果重命名时出现错误)

然后我们创建outdirectory(如果dir已经存在,它将发出警告,但是不会覆盖任何内容。我们可以首先测试outdir是否已经存在,如果这样,则忽略dir.create

最后,我们使用file.copy将重命名的文件复制到outdirectory。我们可以使用file.path连接目录和文件名。

# rename all the files using their PiD_treatmentsum 
# and copy them to outdirectory
filenames <- list.files(".", pattern="*M\\.txt")
outdirectory <- "~/out"
lapply(filenames, function(x) {
  df <- read.csv(x, sep="\t", header=TRUE, fill = T,stringsAsFactors = F)
  a <- as.character(unique(df[["PiD_treatmentsum"]]))
  b<-paste0("ctrl_",a, '.txt', sep="")
    newname <- file.rename(basename(x), b)
    dir.create(outdirectory)
    file.copy(b, file.path(outdirectory, b))
})
© www.soinside.com 2019 - 2024. All rights reserved.