Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将数据帧从R导出到excel并用空行分隔_R_Excel_Append_Export - Fatal编程技术网

如何将数据帧从R导出到excel并用空行分隔

如何将数据帧从R导出到excel并用空行分隔,r,excel,append,export,R,Excel,Append,Export,我应该在R中附加存储在不同列表中的数据帧,并用一个空行(在excel文件中)分隔它们 我无法将不同列表中的数据帧绑定到一个列表中,因为它们的列数不同 我不能同时使用“xlsx”和“XLConnect”包,因为它们会给我带来与Java相关的问题 欢迎任何帮助 数据帧的第一个列表: listofdfs <- list(x <- data.frame("y"=c(2009,2010,2011),"b"=c(35,30,20)), y <- da

我应该在R中附加存储在不同列表中的数据帧,并用一个空行(在excel文件中)分隔它们

我无法将不同列表中的数据帧绑定到一个列表中,因为它们的列数不同

我不能同时使用“xlsx”和“XLConnect”包,因为它们会给我带来与Java相关的问题

欢迎任何帮助

数据帧的第一个列表:

listofdfs <- list(x <- data.frame("y"=c(2009,2010,2011),"b"=c(35,30,20)), y <- data.frame("y"=c(2009,2010,2011), "b"=c(6,21,40)) )

label <- c("Red","Green")

listofdfs <- setNames(listofdfs, label)

$Red
     y  b
1 2009 35
2 2010 30
3 2011 20

$Green
     y  b
1 2009  6
2 2010 21
3 2011 40
listofds一次解决一个问题:

1。添加具有不同列的数据帧

rbind(df1,df2) #this fails if they do not have the same columns

library(dplyr)
df1 %>%
    bind_rows(df2) #this works even with different columns

#including an empty row
df1 %>%
    bind_rows(df_empty) %>% #pre-create a df with one "check" column and an empty row
    bind_rows(df2)
2.编写excel文件

如果您尝试了不同的软件包,如
foreign
,只需将其另存为
csv
,即可在Excel中打开

write.csv(df_result,“result.csv”)

使用
openxlsx
,我希望它比您提到的其他软件包更适合您,您可以:

library(openxlsx)

# create your workbook
mywb <- createWorkbook() 

# create the sheets you need based on the first list of tables
for (sheetName in names(listofdfs)){
    addWorksheet(mywb , sheetName )
}

# get all your lists of tables in a single list
l_listOfDF <- mget(ls(pattern="listofdf"))

# initiate the index of the row where you will want to start writing (one per sheet)
startR <- rep(1, length(listofdfs)) ; names(startR) <- names(listofdfs)

# loop over the lists of tables using index and then over the elements / sheets using their names
for(N_myListOfDF in seq(l_listOfDF)){

  for(pageName in names(l_listOfDF[[N_myListOfDF]])){

    # write the name/number of your table in the correct sheet, at the correct row
    writeData(mywb, sheet=pageName, startRow=startR[pageName], paste0("Table ", N_myListOfDF, "."))

    # write your data in the correct sheet at the correct row (the one after the name)
    writeData(mywb, sheet=pageName, startRow=startR[pageName]+1, l_listOfDF[[N_myListOfDF]][[pageName]])
    
    # update the row number (the + 3 is to leave space for name of table, headers and blank row
    startR[pageName] <- startR[pageName]+nrow(l_listOfDF[[N_myListOfDF]][[pageName]]) + 3

  }
}

# save your workbook in a file
saveWorkbook(mywb , "myfile.xlsx")
库(openxlsx)
#创建工作簿

mywb我需要将不同列表中的数据集按列追加,而不是按与问题描述不匹配的行追加。如果
cbind()
,不同的列并不重要,只是行数不同而已。此外,没有办法在附加列之间添加空的“行”。你能用输入和输出的例子来扩展你的问题描述吗?我用输入和输出的例子编辑了我的问题。有什么可能的解决办法吗?
library(openxlsx)

# create your workbook
mywb <- createWorkbook() 

# create the sheets you need based on the first list of tables
for (sheetName in names(listofdfs)){
    addWorksheet(mywb , sheetName )
}

# get all your lists of tables in a single list
l_listOfDF <- mget(ls(pattern="listofdf"))

# initiate the index of the row where you will want to start writing (one per sheet)
startR <- rep(1, length(listofdfs)) ; names(startR) <- names(listofdfs)

# loop over the lists of tables using index and then over the elements / sheets using their names
for(N_myListOfDF in seq(l_listOfDF)){

  for(pageName in names(l_listOfDF[[N_myListOfDF]])){

    # write the name/number of your table in the correct sheet, at the correct row
    writeData(mywb, sheet=pageName, startRow=startR[pageName], paste0("Table ", N_myListOfDF, "."))

    # write your data in the correct sheet at the correct row (the one after the name)
    writeData(mywb, sheet=pageName, startRow=startR[pageName]+1, l_listOfDF[[N_myListOfDF]][[pageName]])
    
    # update the row number (the + 3 is to leave space for name of table, headers and blank row
    startR[pageName] <- startR[pageName]+nrow(l_listOfDF[[N_myListOfDF]][[pageName]]) + 3

  }
}

# save your workbook in a file
saveWorkbook(mywb , "myfile.xlsx")