gpt4 book ai didi

r - 如何使用 imap 和 map 将列表列表写入 excel

转载 作者:行者123 更新时间:2023-12-04 14:57:21 25 4
gpt4 key购买 nike

我有一个列表列表,我想将它们输入到多个 excel 文件中,其中 excel 名称将是子列表名称,excel 中每个工作表的名称将是每个子列表中的 df 名称。我试图利用 imapmap 来实现我的目标。然而,我是新手,仍然对如何正确设置感到困惑。

示例列表可以使用

构建
lst1<-list(`101-01-101` = list(Demographics = structure(list(SubjectID = c("Subject ID", 
"101-01-101"), BRTHDTC = c("Birthday", "1953-07-07"), SEX = c("Gender",
"Female")), row.names = c(NA, -2L), class = c("tbl_df", "tbl",
"data.frame")), DiseaseStatus = structure(list(SubjectID = c("Subject ID",
"101-01-101"), DSDT = c("DS Date", "2016-03-14"), DSDT_P = c("DS Date Prob",
NA)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))), `101-02-102` = list(Demographics = structure(list(SubjectID = c("Subject ID",
"101-02-102"), BRTHDTC = c("Birthday", "1963-07-02"), SEX = c("Gender",
"Female")), row.names = c(NA, -2L), class = c("tbl_df", "tbl",
"data.frame")), DiseaseStatus = structure(list(SubjectID = c("Subject ID",
"101-02-102"), DSDT = c("DS Date", "2017-04-04"), DSDT_P = c("DS Date Prob",
NA)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))), `101-03-103` = list(Demographics = structure(list(SubjectID = c("Subject ID",
"101-03-103"), BRTHDTC = c("Birthday", "1940-09-11"), SEX = c("Gender",
"Male")), row.names = c(NA, -2L), class = c("tbl_df", "tbl",
"data.frame")), DiseaseStatus = structure(list(SubjectID = c("Subject ID",
"101-03-103"), DSDT = c("DS Date", NA), DSDT_P = c("DS Date Prob",
"UN-UNK-2015")), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))), `101-04-104` = list(Demographics = structure(list(
SubjectID = c("Subject ID", "101-04-104"), BRTHDTC = c("Birthday",
"1955-12-31"), SEX = c("Gender", "Male")), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame")), DiseaseStatus = structure(list(
SubjectID = c("Subject ID", "101-04-104"), DSDT = c("DS Date",
"2016-05-02"), DSDT_P = c("DS Date Prob", NA)), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))), `104-05-201` = list(
Demographics = structure(list(SubjectID = c("Subject ID",
"104-05-201"), BRTHDTC = c("Birthday", "1950-12-04"), SEX = c("Gender",
"Female")), row.names = c(NA, -2L), class = c("tbl_df", "tbl",
"data.frame")), DiseaseStatus = structure(list(SubjectID = c("Subject ID",
"104-05-201"), DSDT = c("DS Date", "2018-07-06"), DSDT_P = c("DS Date Prob",
NA)), row.names = c(NA, -2L), class = c("tbl_df", "tbl",
"data.frame"))))

我目前的代码是:

lst1 %>% imap ( ~  wb = createWorkbook()
Map(function(data, nameofsheet){
addWorksheet(wb, nameofsheet)
writeData(wb, nameofsheet, data)
}, .x, names(.x))

saveWorkbook(wb, file.path("C:/Users/SQ/Documents/",
sprintf("subject_%s.xlsx", .y)))
)

我的代码不起作用,但希望可以让您了解我尝试做的事情。我需要以这种方式完成此操作,因为我需要向工作表添加一些格式。谁能给我一些关于如何执行此操作的指导?

我可能没有正确使用.x & .y。当我们使用列表时,我仍然对如何定义它们感到困惑。

非常感谢。

最佳答案

我们只需要对代码进行简单的调整

  1. 如果我们使用多个表达式,请将其阻止在 {}
  2. 进入 Mapdata 应该是 .x 而不是 .y 作为 。 y 将是 list
  3. 的名称

library(openxlsx)
library(purrr)
library(dplyr)
lst1 %>%
imap ( ~ {
wb <- createWorkbook()
Map(function(data, nameofsheet){
addWorksheet(wb, nameofsheet)
writeData(wb, nameofsheet, data)
}, .x, names(.x))

saveWorkbook(wb, file.path("C:/Users/SQ/Documents/",
sprintf("subject_%s.xlsx", .y)))
}
)

-生成的输出文件

enter image description here

-文件内容

enter image description here

关于r - 如何使用 imap 和 map 将列表列表写入 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67695617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com