简体   繁体   English

for 循环加载多个 RData 文件

[英]for loop to load multiple RData files

I am new to R. I am trying to load multiple RData files from a single folder using a for-loop, however my below code only loads the last iteration.我是 R 新手。我正在尝试使用 for 循环从单个文件夹加载多个 RData 文件,但是我下面的代码仅加载最后一次迭代。 I am struggling to understand the issue, any solutions or guidance would be greatly appreciated.我正在努力理解这个问题,任何解决方案或指导将不胜感激。

myfiles <- list.files("Data/","*.RData", full.names="TRUE")
for (i in 1:length(myfiles)) {
   load(myfiles[i])
}

The data frame objects in each file were of the same name so it was getting replaced with each iteration.每个文件中的数据框对象都具有相同的名称,因此每次迭代都会替换它。

I was using R script to load the data frames from each file into Power Bi.我使用 R 脚本将每个文件中的数据帧加载到 Power Bi。 To do this I had to assign a different name to the data frame object on each iteration.为此,我必须在每次迭代时为数据框对象分配一个不同的名称。 The following solution uses the load.RData function to easily achieve this.下面的解决方案使用 load.RData 函数来轻松实现这一点。

library(miceadds)
myfiles = list.files("Data/","*.RData", full.names="TRUE")
j <- 1
for (i in 1:length(myfiles)){
  load.Rdata(myfiles[i], "df")
  assign(paste("df", j, sep= ""),df)
  j = j+1
  load(myfiles[i])
}
rm(df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM