r - How to reverse the order of data.frame object in the list that let them have same pattern? -


i have 3 list has data.frame object different order fore sure. want let them have same order either list1, list2 or list 3. how can reverse order of data.frame object in different list has same pattern/order? give me possible idea doing sort of manipulation easily? lot.

with variable in list, can use public data airquality, iris, cars, co2 quick run in r;

data

list1 <- list(pass=list(alpha.df1_yes, beta.df1_yes, gamma.df1_yes, theta.df1_yes),               fail=list(alpha.df1_no, beta.df1_no, gamma.df1_no, theta.df1_no))  list2 <- list(pass=list(beta.df2_yes, alpha.df2_yes, gamma.df2_yes, theta.df2_yes),               fail=list(beta.df2_no, alpha.df2_no, gamma.df2_no, theta.df2_no))  list3 <- list(pass=list(gamma.df3_yes, alpha.df3_yes, beta.df3_yes, theta.df3_yes),               fail=list(gamma.df3_no, alpha.df3_no, beta.df3_no, theta.df3_no))  list4 <- list(pass=list( theta.df4_yes, alpha.df4_yes, beta.df4_yes,gamma.df4_yes),               fail=list(theta.df4_no, alpha.df4_no, beta.df4_no,gamma.df4_no )) 

desired output (let's have same pattern list1 does):

list1 <- list(pass=list(alpha.df1_yes, beta.df1_yes, gamma.df1_yes, theta.df1_yes),               fail=list(alpha.df1_no, beta.df1_no, gamma.df1_no, theta.df1_no))  list2_new <- list(pass=list(alpha.df2_yes, beta.df2_yes, gamma.df2_yes, theta.df2_yes),                   fail=list(alpha.df2_no, beta.df2_no, gamma.df2_no, theta.df2_no))  list3_new <- list(pass=list(alpha.df3_yes, beta.df3_yes,gamma.df3_yes, theta.df3_yes),               fail=list(alpha.df3_no, beta.df3_no, gamma.df3_no, theta.df3_no))  list4_new <- list(pass=list(alpha.df4_yes, beta.df4_yes,gamma.df4_yes, theta.df4_yes),               fail=list(alpha.df4_no, beta.df4_no, gamma.df4_no, theta.df4_no)) 

because data in nested structure, bit of tricky let them have same pattern in new constructed list. can propose possible ideas?

i assume nesting constant , create functions keep readability:

exchangedf <- function(list, neworder){     return(list[neworder]) } neworder <- c(2,1,3,4) list2_new <- list(pass = exchangedf(list2$pass, neworder),                   fail = exchangedf(list2$fail, neworder)) #.... 

to make more general:

neworder <- c(2,1,3,4) exchangenesteddf <- function(listoflistsofdf, neworder){   newlist <- lapply(listoflistsofdf, function(x) x[neworder])   names(newlist) <- names(listoflistsofdf)   return(newlist) } list2_new <- exchangenesteddf(list2, neworder) 

and if want can apply list items (especially if have more of them):

neworders <- list(c(2,1,3,4),  c(2,3,1,4),  c(2,3,4,1)) list <- lapply(1:3, function(i) exchangenesteddf(get(paste0("list", i)), neworders[[i]])) 

note solution specific nested lists data.frames.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -