循环以将R中的多个数据框导出到Excel并命名每个工作表 [英] Loop to export multiple dataframes in r to Excel and Name Each Sheet

查看:79
本文介绍了循环以将R中的多个数据框导出到Excel并命名每个工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要导出到Excel的数据框,每个数据框都在不同的工作表上,并为每个工作表命名.

I have 2 data frames that I would like to export to Excel, with each dataframe on a different worksheet, and to title each sheet.

我已经尝试过使用 write.xlsx 进行以下操作.

I have tried the following with write.xlsx .

这是我的2个数据框的列表,其后是我想在新的Excel工作簿中给它们的标题:

Here is the list of my 2 data frames followed by the title I would like to give them in the new Excel workbook:

list_of_datasets <- list(pivot.diagnosis, stat.pivot.quarter)
wk.title <- c("9-STD Durations", "10-STD Clinical Categories")

这是我尝试过的循环:

for (i in length(list_of_datasets)) {
  write.xlsx(list_of_datasets[i], file = "Trial.xlsx", 
           sheetName = wk.title[i], 
           append = TRUE)
}

问题::这仅将 last 数据帧(即 stat.pivot.quarter )输出到Trial.xlsx电子表格. Append = TRUE 存在,所以我认为它将在循环的每次迭代后附加 ,但事实并非如此. append = T 似乎无效

PROBLEM: This only outputs the last data frame (i.e. stat.pivot.quarter) to the Trial.xlsx spreadsheet. Append = TRUE is there, so I would think it would append with each iteration of the loop, but it's not. It seems like append = T isn't working

任何潜在客户均表示赞赏!

Any leads appreciated!

推荐答案

writexl 是轻量级的可以在这里为您服务的软件包.

writexl is a lightweight package that would serve you well here.

writexl::write_xlsx(
  list("sheetname1" = dataframe1, "sheetname2" = dataframe2),
  path = "path/to/save/to.xlsx"
)

这篇关于循环以将R中的多个数据框导出到Excel并命名每个工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆