list2<-list(b1=c("b11","b12"),b2=c("b21","" /> list2<-list(b1=c("b11","b12"),b2=c("b21","" /> list2<-list(b1=c("b11","b12"),b2=c("b21",""/>

lists, use index and keeping their names

17 views Asked by At

I have two lists: >list1<-list(a1=c("a11","a12","a13"), a2=c("a21","a22","a23"), a3=c ("a31","a32","a33")) >list2<-list(b1=c("b11","b12"),b2=c("b21","b22")) > names(list1)
[1] "a1" "a2" "a3" > names(list2) [1] "b1" "b2" I want each one of the elements of the list to be combined with each one of the elements of the other list

#make any empty list with their combination that later on enters a for loop. >my_list_2 <- vector("list", length = length(list1)*length(list2))

#using their indexes to combine them >data_fr <- expand.grid(i = seq_along(list2), j = seq_along(list1))[2:1] >data_fr <- apply(data_fr, 1, paste, collapse = "_") >names(my_list_2) <- data_fr

What I get is: > data_fr [1] "1_1" "1_2" "2_1" "2_2" "3_1" "3_2".

What I would like to get is "1_b1", "1_b2", "2_b1", "2_b2", "3_b1", "3_b2". I would like to keep the name of the second list or both of them. Any good ideas or suggestions? Thank you in advance.

0

There are 0 answers