I have a list of list, and each sublist also have multiple df. Now I would like to know the number of cols in each df in each sublist using imap
. How can I get point to the df correctly.
Sample list can be built using:
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"))))
I tried to use two imap to get to that level, but lost the direction at the end. Could someone help me on this and tell me how to correctly point the df in sublist.
my codes is sth like this:
imap ( ~ {
wb = createWorkbook()
imap(.x, ~ {
addWorksheet(wb, .y)
writeData(wb, .y, .x)
setColWidths(wb, .y, cols = 1:ncol(.x), widths = "auto")
})
saveWorkbook(wb, file.path("C:/Users/",
sprintf("subject_%s.xlsx", .y)))
}
)
Update:
if the df in sublist contain sth like this: