Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reorder multiple df in a list

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)))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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, ",", ";"))))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading