问题描述
我正在尝试加载保存在同一目录中的多个.rds文件。我已经为此创建了一个函数,并在文件目录列表中进行了迭代以加载它,但是它不起作用,请参见下面的内容:
markerDir="..."
markerFilesList <- list.files(markerDir,pattern = ".rds",recursive = TRUE,include.dirs = TRUE)
readRDSfct <- function(markerFilesList,markerDir,i){
print(paste0("Reading the marker file called :",basename(markerFilesList[[i]])))
nameVariableTmp=basename(markerFilesList[[i]])
nameVariable=gsub(pattern = "\\.rds",'',nameVariableTmp)
print(paste0("file saved in varibale called:",nameVariable))
currentRDSfile = readRDS(paste0(markerDir,markerFilesList[[i]])) #nameVariable
return(currentRDSfile)
}
for (i in 1:length(markerFilesList)){
readRDSfct(markerFilesList,i)
}
有人建议我这样做吗?
提前谢谢!
解决方法
据我所知,您只想加载所有RDS,并将它们保存在R环境中的同一目录中?
要将所有.RDS加载并绑定到一个目录中,我使用的是这样的内容:
List_RDS = list.files(pattern="*.RDS")
List_using = lapply(List_RDS,readRDS)
Data_bind <-do.call("rbind",List_using)