Home > Enterprise >  How to reorder multiple df in a list
How to reorder multiple df in a list

Time:11-24

I have a list of df. I would like to sort each df in lst using the info from Order. I try to use map or map2 to do this. however my codes doesn't work. Any suggestion?

I would like to know how to achive this goal using map, or lapply if that is doable. Thanks.

## Sample list
lst1<- list(Test = structure(list(SubjectID = c("101-05-202", "101-06-203", 
"102-10-401", "101-11-402", "105-12-403", "101-13-501", "101-14-502", 
"101-15-503", "101-01-101", "101-05-202", "101-06-203", "101-11-402", 
"101-13-501", "101-14-502", "101-15-503", "101-01-101", "101-05-202", 
"101-06-203", "101-11-402", "101-13-501", "101-14-502", "101-15-503", 
"101-01-101", "101-05-202", "101-06-203", "101-11-402", "101-13-501", 
"101-14-502", "101-15-503", "101-05-202", "101-06-203", "101-11-402", 
"101-13-501", "101-14-502", "101-15-503", "101-06-203", "101-11-402", 
"101-13-501", "101-14-502", "101-15-503", "101-11-402", "101-13-501", 
"101-14-502", "101-15-503", "101-06-203", "101-11-402", "101-14-502", 
"101-06-203", "101-14-502", "101-06-203", "101-06-203", "101-06-203", 
"101-06-203", "101-06-203", "101-06-203"), Test = c("ELA", 
"ELA", "ART", "ELA", "ELA", "ELA", "ELA", "ELA", "ELA, PE", "ELA", 
"ELA", "ELA", "ELA, MATH", "ELA", "ELA", "ELA,PE", "ELA", "ELA", 
"ELA", "ELA", "ELA", "ELA", "PE", "ELA", "ELA", "ELA", "ELA", 
"ELA", "ELA", "ELA", "ELA", "ELA", "ELA,MATH", "ELA", "ELA", 
"ELA", "ELA", "ELA", "ELA", "ELA", "ELA", "ELA", "ELA", "ELA", 
"ELA", "ELA", "ELA", "ELA", "ELA", "ELA", "ELA", "ELA", "ELA", 
"ELA,PE", "ELA")), class = "data.frame", row.names = c(NA, -55L
)), Comments = structure(list(SubjectID = c("101-01-101", "101-02-102", 
"106-03-103", "101-05-202", "103-04-201", "103-09-303", "105-12-403"
), COMYN = c("No", "No", "No", "No", "No", "No", "Yes")), class = "data.frame", row.names = c(NA, 
-7L)), Enroll = structure(list(SubjectID = c("101-01-101", "101-02-102", 
"106-03-103", "101-05-202", "103-04-201", "101-06-203", "102-07-301", 
"105-08-302", "103-09-303", "102-10-401", "101-11-402", "105-12-403"
), EDT = c("09/03/2020", "09/11/2020", "10/08/2020", "12/08/2020", 
"12/09/2020", "12/23/2020", "02/02/2021", "02/17/2021", "03/10/2021", 
"04/19/2021", "04/28/2021", "06/03/2021")), row.names = c(NA, 
-12L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), spec = structure(list(cols = list(SubjectID = structure(list(), class = c("collector_character", 
"collector")), Group = structure(list(), class = c("collector_character", 
"collector")), Site = structure(list(), class = c("collector_character", 
"collector")), SubjectStatus = structure(list(), class = c("collector_character", 
"collector")), Visit = structure(list(), class = c("collector_character", 
"collector")), Form = structure(list(), class = c("collector_character", 
"collector")), FormEntryDate = structure(list(), class = c("collector_logical", 
"collector")), ICDT = structure(list(), class = c("collector_character", 
"collector")), ICDT_CODED = structure(list(), class = c("collector_logical", 
"collector")), ICPVDATE = structure(list(), class = c("collector_character", 
"collector")), ICPVDATE_CODED = structure(list(), class = c("collector_double", 
"collector")), Informed_C_CREATE_DTM = structure(list(), class = c("collector_character", 
"collector")), Informed_C_LAST_UPD_DTM = structure(list(), class = c("collector_character", 
"collector")), SubjectVisitFormID = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec")))

## Order file
Order<-structure(list(File = c("Test", "Comments", "Enroll"), Order = c("SubjectID", 
"SubjectID", "SubjectID, mdy(EDT)")), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))

## Order lst with Order
lst[Order$File] <- map2(lst[Order$File], Order$Order, ~ .x %>% 
                          arrange(paste(.y)))

CodePudding user response:

We may need to parse the expression

library(dplyr)
library(purrr)
library(stringr)
library(lubridate)
lst1[Order$File] <- map2(lst1[Order$File], Order$Order,
      ~ .x %>%
        arrange(!!! rlang::parse_exprs(str_replace(.y, ",", ";"))))
  •  Tags:  
  • r
  • Related