从 R 中的数据帧列表写入 xlsx 文件

问题描述 投票:0回答:1

假设我们有一个名为

list_data
的数据框列表。

我们的目标是为每个

.xlsx
编写一个单独的
openxlsx::write.xlsx()
文件,其中每个文件都有两个以
District
中的 data.frames 命名的子表(也称为选项卡)(在本例中,这些名称是
list_data
one
)。
因为某些 

two

仅包含在

District
one
中,对于此类
two
,其中一个子表将为空,这是可以的。
文件名可以是 

District

的名称。

我尝试了以下方法但没有成功。这在 R 中可能吗?

District


r dataframe function loops xlsx
1个回答
0
投票

library(tidyverse) library(openxlsx) list_data<- list(one= data.frame(District=LETTERS[1:3], V1 = 1:3), two= data.frame(District=LETTERS[3:8], V7 = 3:8, V3=8:3)) lapply(list_dat,\(j) group_by(j,District) %>% write.xlsx(file =paste0(j$District,".xlsx"), sheetName = ???)) # Not sure how to add subsheet names here

© www.soinside.com 2019 - 2024. All rights reserved.