我在写一个函数来读取多个.xlsx文件时遇到了一些麻烦,这些文件是R中带有for循环的单独数据帧。当我运行该功能时没有任何反应。没有错误,但也没有数据帧加载到R中。当我从函数中取出赋值片段并手动将for循环的输入更改为示例时,assign函数有效。以下是代码示例:
library(readxl)
Load<-function(File_Path,Samp){
setwd(File_Path)
for (i in 1:length(Samp)){
assign(paste("Sample_",Samp[i],sep = ""),read_excel(paste("Sample_",Samp[i],".xlsx",sep = "")))
}
}
Load(File_Path = "~/Desktop/Test",Samp = "A") # Doesn't Work
#When this piece is taken out of the loop and the Sample ("A") replaced it works.
assign(paste("Sample_","A",sep = ""),read_excel(paste("Sample_","A",".xlsx",sep = ""))) # Does Work
实际上,有一长串样本需要加载,并希望通过将一个列表分配给&#34; Samp&#34;例如c(&#34; A&#34;,&#34; C&#34;,&#34; D&#34;)。预先感谢任何帮助。
答案 0 :(得分:1)
您可以通过将inherits=TRUE
添加到assign