我在不同的目录中有一组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]],可选= TRUE)出错:不能 coerce class“structure(”jobjRef“,package =”rJava“)”to data.frame
我有什么遗漏的东西吗?
答案 0 :(得分: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
;它只是用于从单个数据框创建工作簿。