I have a simple problem but cannot find a simple answer for it. I have the following list type
my.list=list(a=c(1,2,3),random=1,list=matrix(1:10,nrow=2))
of which I would like to change the order of the list items (here: "a", "random" and "list") in accordance with the order of another list ("a", "list", "random"), or, thus, the vector list names: names (my.other.list):
my.other.list=list(a=c(4,5,6),list=matrix(101:110,nrow=2),random=2)
therefore, the goal is to get this list:
my.wanted.list=list(a=c(1,2,3),list=matrix(1:10,nrow=2),random=1)
Please note that both lists (my.list and my.other.list) have the same list names and that they cannot be sorted alphabetically or so.
I created this loop:
my.wanted.list=list() for(i in 1:length(my.list)){ my.wanted.list[[i]]=my.list[[names(my.other.list)[i]]] } names(my.wanted.list)=names(my.other.list)
But this seems like a time-consuming solution to a seemingly simple problem. Especially if the lists are larger. So is there an easier way to do this?
Thanks.