Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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 - Fatal编程技术网

R 将数据框列表写入多张表格中的Excel

R 将数据框列表写入多张表格中的Excel,r,excel,R,Excel,我有一个数据框,我运行了一些分析,我想把结果导出到Excel文件。每组一份工作簿,每个分析结果在单独的选项卡上。我更愿意使用openxlsx进行导出,以将java从等式中剔除 library(plyr) library(dplyr) library(openxlsx) df <- iris # Analysis 1 results1 <- df %>% group_by(Species) %>% summarise(count = n()) # Analys

我有一个数据框,我运行了一些分析,我想把结果导出到Excel文件。每组一份工作簿,每个分析结果在单独的选项卡上。我更愿意使用
openxlsx
进行导出,以将java从等式中剔除

library(plyr)
library(dplyr)
library(openxlsx)

df <- iris

# Analysis 1
results1 <- df %>%
  group_by(Species) %>%
  summarise(count = n())

# Analysis 2
results2 <- df %>%
  group_by(Species) %>%
  summarise(mean.sl = mean(Sepal.Length),
            mean.sw = mean(Sepal.Width))
其他建议?

示例代码

library(plyr)
library(dplyr)
library(openxlsx)


setwd("c:/r")
df <- iris

# Analysis 1
results1 <- df %>%
  group_by(Species) %>%
  summarise(count = n())

# Analysis 2
results2 <- df %>%
  group_by(Species) %>%
  summarise(mean.sl = mean(Sepal.Length),
            mean.sw = mean(Sepal.Width))

#get the unique species
sp <- unique(df$Species)

createSpreadsheets <- function(species,r1,r2){
  ## Create new workbooks
  wb <- createWorkbook() 

  ## Create the worksheets
  addWorksheet(wb, sheetName = "Results1" )
  addWorksheet(wb, sheetName = "Results2" )

  ## Write the data
  writeData(wb, "Results1", r1)
  writeData(wb, "Results2", r2)

  ## Save workbook to working directory 
  saveWorkbook(wb, file = paste(species,".xlsx", sep=""), overwrite = TRUE)
}

## create spreadsheets by calling our function for each species
for(s in sp){
  createSpreadsheets(s,results1[results1$Species==s,],results2[results2$Species==s,])
}
库(plyr)
图书馆(dplyr)
库(openxlsx)
setwd(“c:/r”)
df%
总结(计数=n()
#分析2
结果2%
组别(种类)%>%
总结(平均值sl=平均值(萼片长度),
平均值sw=平均值(萼片宽度))
#获得独特的物种

sp我的建议是不要使用Excel,虽然可能没有什么帮助。另外,我相信您要找的是
xlsx
依赖于java,如果可能的话,我会尽量避免使用java。WriteXLS包对我来说工作得很好。只需将data.frames的命名列表传递给它,这些将成为结果中的选项卡。
library(plyr)
library(dplyr)
library(openxlsx)


setwd("c:/r")
df <- iris

# Analysis 1
results1 <- df %>%
  group_by(Species) %>%
  summarise(count = n())

# Analysis 2
results2 <- df %>%
  group_by(Species) %>%
  summarise(mean.sl = mean(Sepal.Length),
            mean.sw = mean(Sepal.Width))

#get the unique species
sp <- unique(df$Species)

createSpreadsheets <- function(species,r1,r2){
  ## Create new workbooks
  wb <- createWorkbook() 

  ## Create the worksheets
  addWorksheet(wb, sheetName = "Results1" )
  addWorksheet(wb, sheetName = "Results2" )

  ## Write the data
  writeData(wb, "Results1", r1)
  writeData(wb, "Results2", r2)

  ## Save workbook to working directory 
  saveWorkbook(wb, file = paste(species,".xlsx", sep=""), overwrite = TRUE)
}

## create spreadsheets by calling our function for each species
for(s in sp){
  createSpreadsheets(s,results1[results1$Species==s,],results2[results2$Species==s,])
}