0

I have three list which has data.frame object but with very different order fore sure. I want to let them have same order either like list1, list2 or list 3. How can I reverse the order of data.frame object in different list has same pattern/order? Does anyone give me some possible idea for doing this sort of manipulation easily? Thanks a lot.

with variable in the list, we can use public data like airquality, iris, cars, CO2 for 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 with 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 are in the nested structure, it is bit of tricky to let them have same pattern in the new constructed list. Can anyone propose possible ideas?

jyson
  • 245
  • 1
  • 8
  • 27
  • After saving the data.frames into the list, they will not have names (not in the way you saved them), so I don't know how to get the correct order. But if you have the correct order the following will work: ``list2_New$pass <- list2_New$pass[c(2,1,3,4)]``. If this is appropriate, I can post a complete answer. – Phann Jul 28 '16 at 11:25
  • True, it is better for me to get out nested structure, but like what you said, split list2_New in list2_New$pass, list2_New$fail, that would be good idea. Could you elaborate your answer more specifically, so I can try it in my machine to examine whether the result fit or not. Thx – jyson Jul 28 '16 at 11:37
  • @Imo : for sure, I tested your solution, it was very well implemented. – jyson Jul 28 '16 at 13:43

1 Answers1

1

I assume that the nesting is constant and create some help functions to 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 it 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 you want you can apply that to all list items (especially if you 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 that this solution is very specific for your nested lists with data.frames.

Phann
  • 1,283
  • 16
  • 25