【问题标题】:Importing and merging files in loop循环导入和合并文件
【发布时间】:2019-12-04 17:52:22
【问题描述】:

我必须合并数据集。它们是 .sav 文件,我每个月每年有 6-7 个数据集——总共 13 年。这是要导入和组合的大量数据集,我想使用循环自动执行此操作。

由于我是初学者,我编写了第一个循环来简单地组合数据集一年(所以只循环几个月)。这是我的代码,它完美地完成了我想要的。它不是最快的,当然也不是最漂亮或最高效的,但它确实有效。注意:为简洁起见,我在发布的代码中缩短了“C ...”路径:在我的真实代码中,它是完整路径。

 for (m in months) {
setwd(paste("C:.... survey\\DANE 2005\\",m,sep=""))
files_2005 <- list.files(path=(paste("C:\....survey\\DANE 2005\\",m,sep="")),  pattern=("Area.*.sav"))

#for (i in (paste("files_",m,sep=""))){
   df_2005 <- lapply(files_2005, read_sav)
  assign(paste("DANE2005_",m,sep=""), df_2005 %>% reduce(rbind.fill))

#}

df_2005 <- mget(ls(pattern="DANE2005_"))
dane_2005 <- df_2005 %>% reduce(rbind.fill)

}

这是我当前的代码,循环数年和数月(感谢@Onyambu 的 cmets)。但是,它仍然不起作用;如果我不使用 setwd R 表示“当前文件在目录中不存在”(并指回我的主目录,而不是指定的路径)。如果我确实使用 setwd,我会收到“无法更改工作目录”错误。

for (y in years) {
  for (m in months) {

    #Go to a folder per year/month
    path <- paste("C:.... survey\\DANE ",y,"\\",m,sep="")
    #Create a list of all the files in that folder by month, based on a pattern
    list_data<-list.files(path=path,  pattern=("Area.*.sav"))

    if(!is_empty(list_data)){
    #Read in all the files in the folder by month, based on the list
    df_2005 <- lapply(list_data, read_sav)
    #bind the files for one month together based on the list
    assign(paste("DANE2005_",m,sep=""), df_2005 %>% reduce(rbind.fill))
    }
  }
  #Bind together all the files for one year
  df_2005 <- mget(ls(pattern="DANE2005_"))
  dane_2005 <- df_2005 %>% reduce(left_join)
}

非常感谢任何帮助。

编辑:清理代码并在初始 cmets 之后重新提出问题以清楚起见。

【问题讨论】:

  • 首先你的整个代码只是一个重复。 path &lt;-"C:...survey 2005"。您不需要 setwd 使用 `list.dirs(path,full.names = TRUE,recursive=TRUE)` 现在使用 lapply 将其读取到您的列表中。就这些
  • @Onyambu,谢谢!我不知道 list.dirs。似乎确实非常适合这个问题。但你到底是什么意思?我应该:1)设置路径(按原样),然后 2)单独添加assign(assign(paste("df_",y,sep="")),(list.dirs(path, full.names=T,recursive=T))),或者我应该替换您建议的代码来代替这段代码中的粘贴:lapply(paste("files_",y,sep="")。我尝试了第二个选项,但出现“无法打开文件”错误。再次感谢您!
  • 您不需要使用assign,也不需要使用paste。由于您需要文件夹中的文件,请使用s&lt;-dir(path,recursive=TRUE,full.names=TRUE) 然后使用mydat&lt;-setNames(lapply(s, read.sav),basename(s))。这会将所有文件夹中的所有数据读取到一个列表中。如果要将月份作为列包含,则应编写自定义的 read.sav 函数
  • 我根据您的 cmets 仔细查看了代码并对其进行了编辑。我重新提出了上面的问题,因为它仍然给我错误......
  • 我不太确定你是否理解我所说的。为了有空间,我会给出一个答案,然后你看看它,现在让我来

标签: r loops directory lapply paste


【解决方案1】:

这是你需要尝试的:

# give the path only to the folder where the years are inside the folder
path <- "C:.... survey"

# read all the files in this path using recursive = TRUE-Gives all years,all months,all files
all_files <- list.files(path, pattern = "Area.*.sav", full.names=TRUE, recursive = TRUE)

# Now read all these files into a list. Of course you would like to have the year and the month for the file:

my_read <- function(x){
      nm <- unlist(strsplit(sub(".*survey/","",x),"/"))# Remove everything until survey. You only remain with year,month and file name
      cbind(year = nm[1],month = nm[2],file = nm[3], read_sav(x))
    }

# Now use myl_read function in read the data:

  dat_list <- lapply(all_files,my_read)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2019-07-30
    • 2021-01-22
    • 2010-10-24
    • 2012-05-26
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多