I have the following list of lists:
list_1 <- list(a = 2, b = 3) list_2 <- list(a=c(5,6), b= c(2,3)) list_3 <- list(a=c(10,5,8,1), b=c(9,6,2,9)) list_4 <- list(a=c(2,5,58), b=c(69,6,23)) mylist <- list(list_1, list_2, list_3, list_4) names(mylist)<- c("list_1", "list_2", "list_3", "list_4")
Now I want to extract all the values of a and b from the lists and save them either as data.frame with the names of the corresponding lists as an ID column, for example:
[ID] [a] [b] [1] list_1 2 3 [2] list_2 5 2 [3] list_2 6 3 [4] list_3 10 9 [5] list_3 5 6 [6] list_3 8 2 [7] list_3 1 9 [8] list_4 2 69 [9] list_4 5 6 [10] list_4 58 23
or as variables, such that a contains all the values, b contains all the values of b, and the ID contains the corresponding identifiers of the list:
[a] 2 5 6 10 5 8 1 2 5 58 [b] 3 2 3 9 6 2 9 69 6 23 [ID] "list_1" "list_2" "list_2" "list_3" "list_3" "list_3" "list_3" "list_4" "list_4" "list_4"
I tried the second approach with a for loop, but could not archive the desired result. But even if I could handle it, I don’t know how I can solve the problem with the identifier. It would be great if the solution could be general, because I have many such lists of different lengths.