R-如何动态地命名数据帧? [英] R- how to dynamically name data frames?

查看:130
本文介绍了R-如何动态地命名数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录中有两种文件类型。每种类型都有文本Drug_Rep,如果它不在那里它意味着它的一个控制文件。
药物数据的重复数量可能会有所不同,控制也是如此。
我正在读取for循环中的文件,如下所示。
我想将我的数据框命名为X_Drug_Rep1,然后将下一个命名为X_Drug_Rep2等等,并保存数据框进行进一步处理。
对控件执行相同操作... X_CONTROL_Rep1 ... X_CONTROL_Rep2 ...等等。
什么是将数据保存到数据框中的语法,因为我需要稍后对药物复制数据框和控件进行一些合并和计算。
贴在左侧的作业似乎不起作用。任何建议?

  for(f in 1:length(fileList)){
fileName< - fileList [f ]
X< -read.xls(fileName)

if(regexpr(Drug_Rep,fileName)[1]> 0){
print(DRUG )
print(fileName)
paste(X_Drug_Rep,i)< -X
i = i + 1
}
else {
print )
print(fileName)
贴(X_CONTROL,j)< -X
j = j + 1
}
}


解决方案

OP真的很挣扎,而不是长时间的评论,我会在这里给他看。不要在意这个是否关闭。



技术(不要这样做)会使用分配

  i<  -  1 
j< - 1 $ b $ (f in 1:length(fileList)){
fileName< - fileList [f]
X <-read.xls(fileName)

if(grepl (Drug_Rep,fileName)){
print(DRUG)
print(fileName)
assign(paste(X_Drug_Rep,i,sep ='_'),X)
i< - i + 1
} else {
print(CONTROL)
print(fileName)
assign(paste(X_CONTROL,i,sep ='_'),X)
j< - j + 1
}
}

但是我们建议您使用列表。使用循环,它将如下所示:

  X_Drug_Rep< ;  -  list()
X_CONTROL< - list()
i< - 1
j < - 1
for(f in 1:length(fileList)){
fileName< - fileList [f]
X< -read.xls(fileName)

if(grepl(Drug_Rep,fileName)){
print DRUG)
print(fileName)
X_Drug_Rep [[i]]< - X
i< - i + 1
} else {
print控制)
print(fileName)
X_CONTROL [[j]]< - X
j< - j + 1
}
}

最后,如果没有循环的

  drug.rep.files<  -  grep(Drug_Rep,fileList,value = TRUE)
控制。文件< - grep(Drug_Rep,fileList,value = TRUE,invert = TRUE)

X_Drug_Rep< - lapply(drug.rep.files,read.xls)
X_CONTROL< ; - lapply(control.files,read.xls)

更短,不?再次,这创建了两个列表。例如,而不是 X_Drug_Rep_1 ,您可以通过执行 X_Drug_Rep [[1]] 访问第一个Drug_Rep项目。 / p>

I have two types of files in my directory. Each type has either the text "Drug_Rep" in it or if its not there it means its a control file. Drug data has replicates which can vary in number and so does the control. I am reading the files in a for loop as follows. I want to name my dataframes as X_Drug_Rep1 and then the next as X_Drug_Rep2 and so on...and save the dataframe for further processing. Do the same for controls...X_CONTROL_Rep1...X_CONTROL_Rep2...and so on. What is the syntax to save my data into dataframes because i need to do some merges and calculations later on the drug replicate dataframes and controls separately. paste on the left side of assignment does not seem to work. Any suggestions?

for (f in 1:length(fileList)){
    fileName <- fileList[f]
    X <-read.xls(fileName)

    if(regexpr("Drug_Rep", fileName)[1]>0){
      print("DRUG")
      print(fileName)
      paste(X_Drug_Rep,i)<-X
      i=i+1
    }
    else{
      print("CONTROL")
      print(fileName)
      paste(X_CONTROL,j)<-X
      j=j+1
    }
  }

解决方案

OP is really struggling so instead of a long comment, I'll show him here. Don't care if this gets closed.

The technical (don't do that answer) would be to use assign:

i <- 1
j <- 1
for (f in 1:length(fileList)){
    fileName <- fileList[f]
    X <-read.xls(fileName)

    if(grepl("Drug_Rep", fileName)) {
      print("DRUG")
      print(fileName)
      assign(paste("X_Drug_Rep", i, sep = '_'), X)
      i <- i+1
    } else {
      print("CONTROL")
      print(fileName)
      assign(paste("X_CONTROL", i, sep = '_'), X)
      j <- j+1
    }
  }

But as we recommended, you should use lists instead. Using a for loop, it would look like this:

X_Drug_Rep <- list()
X_CONTROL  <- list()
i <- 1
j <- 1
for (f in 1:length(fileList)){
    fileName <- fileList[f]
    X <-read.xls(fileName)

    if(grepl("Drug_Rep", fileName)) {
      print("DRUG")
      print(fileName)
      X_Drug_Rep[[i]] <- X
      i <- i+1
    } else {
      print("CONTROL")
      print(fileName)
      X_CONTROL[[j]] <- X
      j <- j+1
    }
  }

Finally, how your code would look like without a for loop:

drug.rep.files <- grep("Drug_Rep", fileList, value = TRUE)
control.files  <- grep("Drug_Rep", fileList, value = TRUE, invert = TRUE)

X_Drug_Rep <- lapply(drug.rep.files, read.xls)
X_CONTROL  <- lapply(control.files, read.xls)

Much shorter, no?! Again, this creates two lists. For example, instead of X_Drug_Rep_1, you would access the first Drug_Rep item by doing X_Drug_Rep[[1]].

这篇关于R-如何动态地命名数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆