将来自不同列表的同名数据框导出到它们各自的Excel文档中。

huangapple go评论99阅读模式
英文:

Exporting same named Data Frames from separate lists into their own excel documents

问题

我有五个数据框的列表。我尝试编写一个应用函数或for循环,以从这五个列表中提取具有相似名称的数据框,将它们放入单独的电子表格中,然后将它们导出为Excel文档。

所以我从一个包含5个电子表格的大型Excel文档开始(我将其称为“ORIGINAL_DOCUMENT”)。我使用了split函数根据列值创建了列表:

  1. LIST_DF <- ORIGINAL_DOCUMENT %>%
  2. base::split(., .$COMPANY_NAME)

这是我目前在R中看到的一种可视化示例:

  1. |List 1 | List 2 | | List 3 | List 4 | List 5 |
  2. | -------- | -------- | | -------- | -------- | -------|
  3. | DF 1 | DF 1 | | DF 1 | DF 1 | DF 1 |
  4. | DF 2 | DF 2 | | DF 2 | DF 2 | DF 2 |

我希望输出创建一个Excel文档,其中来自每个列表的DF 1保存为一个电子表格,依此类推。对于每个Excel文档,我希望电子表格的名称相同(Company1,Company2,Company3,Company4和Company5)。

我真的会非常感激任何建议或帮助。谢谢!

英文:

I have five lists of Data frames. I am trying to write an apply function or a for loop that will pull similar named Dataframes from the five lists, put them into separate spreadsheets and then export them as excel documents.

So I started with a large excel document with 5 spreadsheets (I'll call it "ORIGINAL_DOCUMENT". I used a split function to create lists based on column value:

LIST_DF &lt;- ORIGINAL_DOCUMENT%&gt;%
base::split(., .$COMPANY_NAME)

Here's a kind of visual example of what I'm seeing in R right now:

  1. |List 1 | List 2 | | List 3 | List 4 | List 5 |
  2. | -------- | -------- | | -------- | -------- | -------|
  3. | DF 1 | DF 1 | | DF 1 | DF 1 | DF 1 |
  4. | DF 2 | DF 2 | | DF 2 | DF 2 | DF 2 |

I would like the output to create an excel document with DF 1 from each list saved as a spreadsheet and so on. For every excel document, I want the spreasheets named the same (Company1, Company2, Company3, Company 4 and Company 5.)

I could really appreaciate any advice/help out there. Thanks!

答案1

得分: 1

以下是翻译好的代码部分:

  1. # 创建一个数据框列表
  2. df_list <- lapply(1:5, function(x) {
  3. data.frame(COMPANY_NAME = c("A", "B"), values = rnorm(2))
  4. })
  5. # 为列表命名
  6. names(df_list) <- paste0("df", 1:5)
  7. # 遍历每个数据框
  8. for (df_name in df_names) {
  9. # 创建一个新的工作簿
  10. wb <- openxlsx::createWorkbook()
  11. # 为每个列表添加工作表
  12. for (i in seq_along(lists)) {
  13. df <- lists[[i]][[df_name]]
  14. openxlsx::addWorksheet(wb, paste0("Company", i))
  15. openxlsx::writeData(wb, paste0("Company", i), df)
  16. }
  17. # 保存工作簿
  18. openxlsx::saveWorkbook(wb, paste0(df_name, ".xlsx"), overwrite = TRUE)
  19. }

希望这对你有所帮助。

英文:

Here is a possibly way how we could do it:

  1. # create a list of dataframes
  2. df_list &lt;- lapply(1:5, function(x) {
  3. data.frame(COMPANY_NAME = c(&quot;A&quot;, &quot;B&quot;), values = rnorm(2))
  4. })
  5. # name list
  6. names(df_list) &lt;- paste0(&quot;df&quot;, 1:5)
  7. # Loop through each data frame
  8. for (df_name in df_names) {
  9. # Create a new workbook
  10. wb &lt;- openxlsx::createWorkbook()
  11. # Add a worksheet for each list
  12. for (i in seq_along(lists)) {
  13. df &lt;- lists[[i]][[df_name]]
  14. openxlsx::addWorksheet(wb, paste0(&quot;Company&quot;, i))
  15. openxlsx::writeData(wb, paste0(&quot;Company&quot;, i), df)
  16. }
  17. # Save the workbook
  18. openxlsx::saveWorkbook(wb, paste0(df_name, &quot;.xlsx&quot;), overwrite = TRUE)
  19. }

将来自不同列表的同名数据框导出到它们各自的Excel文档中。

huangapple
  • 本文由 发表于 2023年6月13日 03:07:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定