在R中创建工作簿时循环遍历多个列表

时间:2014-10-02 14:32:05

标签: r

我最近从这个站点获得了一个R脚本来在R中创建Excel工作簿。它一直很适合我,除了现在我想通过不同的" in"和" out"文件,但我不知道该怎么做。我知道我可能需要创建像

这样的列表
f.in = c("Stata/Results/1st stage p/vrs/",
  "Stata/Results/1st stage p/crs/",
  "Stata/Results/1st stage np/vrs/",
  "Stata/Results/1st stage np/crs/")

f.out = c("Stata/Results/1st stage p/summaryVRS.xlsx",
  "Stata/Results/1st stage p/summaryCRS.xlsx",
  "Stata/Results/1st stage np/summaryVRS.xlsx",
  "Stata/Results/1st stage np/summaryCRS.xlsx")

但无论如何,以下脚本就是我目前所拥有的。我一直在运行脚本,注释掉.in.out文件的不同行,但效率非常低!

require(data.table)
require(XLConnect)

rm(list = ls())

setwd("S:/X productivity")

#folder/where/CSV_files_are_located

#folder = "Stata/Results/1st stage p/vrs/"
#folder = "Stata/Results/1st stage p/crs/"
#folder = "Stata/Results/1st stage np/vrs/"
folder = "Stata/Results/1st stage np/crs/"

#path/to/file.xlsx

#f.out <-  "Stata/Results/1st stage p/summaryVRS.xlsx"
#f.out <-  "Stata/Results/1st stage p/summaryCRS.xlsx"
#f.out <-  "Stata/Results/1st stage np/summaryVRS.xlsx"
f.out <-  "Stata/Results/1st stage np/summaryCRS.xlsx"

## load in file
wb <- loadWorkbook(f.out, create=TRUE)

## get all files
pattern.ext <- "\\.csv$"
files <- dir(folder, full=TRUE, pattern=pattern.ext)

## Grab the base file names, you can use them as the sheet names
files.nms <- basename(files)
files.nms <- gsub(pattern.ext, "", files.nms) #get rid of the .csv extention

i <-0
for (nm in files) {
  ## ingest the CSV file
  temp_DT <- fread(nm)

  ## Create the sheet where the file will be outputed to 
  i <-i+1
  fn <-files.nms[i]
  createSheet(wb, name=fn)

  ## output the csv contents
  writeWorksheet(object=wb, data=temp_DT, sheet=i, header=TRUE, rownames=NULL)
}

saveWorkbook(wb)

1 个答案:

答案 0 :(得分:1)

所以我花了一些时间思考这个问题,经过多次试验和错误,我终于找到了解决方案!我在这里分享我的代码。当我在寻找答案时,我发现许多经验丰富的R用户评论说R的一个优势是它能够避免使用循环。因此,如果你们中的任何一个人能想到其他聪明的方法来遍历2个列表,我很乐意学习它们。

require(data.table)  ## for fast fread() function
require(XLConnect)

rm(list = ls())

setwd("S:/Controller productivity")

infolder = c("Matlab/Results/3rd stage p/vrs/original/",
         "Matlab/Results/3rd stage p/crs/original/",
         "Matlab/Results/3rd stage np/vrs/original/",
         "Matlab/Results/3rd stage np/crs/original/")

outfile =c("Stata/Results/1st stage p/summaryVRS.xlsx",
       "Stata/Results/1st stage p/summaryCRS.xlsx",
       "Stata/Results/1st stage np/summaryVRS.xlsx",
       "Stata/Results/1st stage np/summaryCRS.xlsx")

j <-0
for (inname in infolder) {

  j<-j+1

#folder/where/CSV_files_are_located
folder = inname

#path/to/file.xlsx
f.out <- outfile[j]

## load in file
wb <- loadWorkbook(f.out, create=TRUE)

## get all files
pattern.ext <- "\\.csv$"
files <- dir(folder, full=TRUE, pattern=pattern.ext)

## Grab the base file names, you can use them as the sheet names
files.nms <- basename(files)
files.nms <- gsub(pattern.ext, "", files.nms) #get rid of the .csv extention

i <-0
for (anything in files) {
  ## ingest the CSV file
  temp_DT <- fread(anything)

  ## Create the sheet where the file will be outputed to 
  i <-i+1
  fn <-files.nms[i]
  createSheet(wb, name=fn)

  ## output the csv contents
  writeWorksheet(object=wb, data=temp_DT, sheet=i, header=TRUE, rownames=NULL)
}

saveWorkbook(wb)

}