如何不覆盖R中的文件

pro*_*ton 2 r

我正在尝试将表格从R复制并粘贴到Excel中.请考虑上一个问题中的以下代码:

    data <- list.files(path=getwd())
    n <- length(list)
    for (i in 1:n) 
     {
      data1 <- read.csv(data[i])
      outline <- data1[,2]
      outline <- as.data.frame(table(outline))
      print(outline)   # this prints all n tables
      name <- paste0(i,"X.csv")
      write.csv(outline, name)
      }
Run Code Online (Sandbox Code Playgroud)

此代码将每个表写入单独的Excel文件(即"1X.csv","2X.csv"等).有没有办法将每个表"移动"到某些行而不是每次都重写前一个表?我也试过这段代码:

 output <- as.data.frame(output)
 wb = loadWorkbook("X.xlsx", create=TRUE)
 createSheet(wb, name = "output")
 writeWorksheet(wb,output,sheet="output",startRow=1,startCol=1)
 writeNamedRegion(wb,output,name="output")
 saveWorkbook(wb)
Run Code Online (Sandbox Code Playgroud)

但这并不会将数据帧完全复制到Excel中.

Qku*_*HBH 5

我认为,正如评论中所提到的,要走的路是首先合并R中的数据帧,然后将它们写入(一)输出文件:

# get vector of filenames
filenames <- list.files(path=getwd())

# for each filename: load file and create outline
outlines <- lapply(filenames, function(filename) {
  data <- read.csv(filename)
  outline <- data[,2]
  outline <- as.data.frame(table(outline))
  outline
})

# merge all outlines into one data frame (by appending them row-wise)
outlines.merged <- do.call(rbind, outlines)

# save merged data frame
write.csv(outlines.merged, "all.csv")
Run Code Online (Sandbox Code Playgroud)