5

我在不同的目录中有一组 csv 文件,我想将它们全部放在一个 excel 文件中,每个表都放在一个 excel 表中。

我正在使用 R 和 xlsx 包。

# loading the library
library(xlsx)
rm(list = ls())

# getting the path of all reports (they are in csv format)
restab = system("ls /home/ubuntu/ibasruns/control/*/report",intern = TRUE)

# creating work book
wb <- createWorkbook()


# going through each csv file
for (item in restab)
{
    # making each as a sheet
    sheet <- createSheet(wb, sheetName=strsplit(item,"/")[[1]][6])
    addDataFrame(read.csv(item), sheet)
    # saving the workbook
    saveWorkbook(wb, "AliceResultSummary.xlsx")
}

# finally writing it.
write.xlsx(wb, "AliceResultSummary.xlsx")

但是,在最后一行,我收到以下错误,

as.data.frame.default(x[[i]], optional = TRUE) 中的错误:无法将类“结构(“jobjRef”,包 =“rJava”)”强制转换为 data.frame

有什么我想念的吗?

4

1 回答 1

9

你很接近:

# creating work book
wb <- createWorkbook()


# going through each csv file
for (item in restab)
{
    # create a sheet in the workbook
    sheet <- createSheet(wb, sheetName=strsplit(item,"/")[[1]][6])

    # add the data to the new sheet
    addDataFrame(read.csv(item), sheet)
}

# saving the workbook
saveWorkbook(wb, "AliceResultSummary.xlsx")

这里write.xlsx不需要;它仅用于从单个数据框创建工作簿。

于 2013-11-15T23:26:29.410 回答