I try to write a function to filter a list that has muti df by ID and then add label to each dataset. However my codes did not work. Could anyone take a look of my codes and give me some guidance on what I did wrong? I am not that good working with list using map
.
My codes are:
Indivadual_xlsx<-function(Subject_id){
datalist2 %>% map(~filter(., SubjectID == Subject_id)%>%
setNames(rbind(names(.), x), mapping.var$Label[match(names(.), mapping.var$Variable)])) %>%
write_xlsx(paste0(output_dir,Subject_id,".xlsx"))
}
update:
Before:
after:
mapping.var:
Update:
Here is the sample data, where lst1 is for datalist2, and Label is for mapping.var:
lst<-list(Demographics = structure(list(SubjectID = c("101-01-101",
"101-02-102", "101-03-103", "101-04-104", "104-05-201"), BRTHDTC = c("1953-07-07",
"1963-07-02", "1940-09-11", "1955-12-31", "1950-12-04"), SEX = c("Female",
"Female", "Male", "Male", "Female")), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")), DiseaseStatus = structure(list(SubjectID = c("101-01-101",
"101-02-102", "101-03-103", "101-04-104", "104-05-201"), DSDT = c("2016-03-14",
"2017-04-04", NA, "2016-05-02", "2018-07-06"), DSDT_P = c(NA,
NA, "UN-UNK-2015", NA, NA)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")), Visits = structure(list(SubjectID = c("101-01-101",
"101-02-102", "101-03-103", "101-04-104", "104-05-201"), Visit = c("Screening: -28 Days to Day 1",
"Screening: -28 Days to Day 1", "Screening: -28 Days to Day 1",
"Screening: -28 Days to Day 1", "Screening: -28 Days to Day 1"
), VISND = c(NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")))
Label<-structure(list(Var = c("SubjectID", "BRTHDTC", "SEX", "DSDT",
"DSDT_P", "Visit", "VISND"), label = c("Subject ID", "Birthday",
"Gender", "DS Date", "DS Date Prob", "Date of Visit", "ND Visit"
)), row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"