This can be tricky:
I need to restructure a list containing an unknown number of subscriptions (although 2 in the sample data). However, each sublist contains ID-Column. For each IDin any of the subscriptions, I now need to create a list containing a line in which it IDcorresponds IDin the sublist, but also the corresponding lines in it are siblings.
This is my initial list:
> str(myList1)
List of 2
$ 1:'data.frame': 2 obs. of 5 variables:
..$ ID : num [1:2] 13369 13599
..$ subject: num [1:2] 2 2
..$ gender : num [1:2] 1 1
..$ age : num [1:2] 18 18
..$ score : num [1:2] 30 28
$ 2:'data.frame': 2 obs. of 5 variables:
..$ ID : num [1:2] 13370 14342
..$ subject: num [1:2] 3 3
..$ gender : num [1:2] 1 1
..$ age : num [1:2] 28 28
..$ score : num [1:2] 27 32
This is the result that I hope to get:
> str(myList2)
List of 4
$ 13369:List of 2
..$ 1:'data.frame': 1 obs. of 5 variables:
.. ..$ ID : num 13369
.. ..$ subject: num 2
.. ..$ gender : num 1
.. ..$ age : num 18
.. ..$ score : num 30
..$ 2:'data.frame': 1 obs. of 5 variables:
.. ..$ ID : num 13599
.. ..$ subject: num 2
.. ..$ gender : num 1
.. ..$ age : num 18
.. ..$ score : num 28
$ 13370:List of 2
..$ 1:'data.frame': 1 obs. of 5 variables:
.. ..$ ID : num 14342
.. ..$ subject: num 3
.. ..$ gender : num 1
.. ..$ age : num 28
.. ..$ score : num 27
..$ 2:'data.frame': 1 obs. of 5 variables:
.. ..$ ID : num 13370
.. ..$ subject: num 3
.. ..$ gender : num 1
.. ..$ age : num 28
.. ..$ score : num 32
$ 13599:List of 2
..$ 1:'data.frame': 1 obs. of 5 variables:
.. ..$ ID : num 13369
.. ..$ subject: num 2
.. ..$ gender : num 1
.. ..$ age : num 18
.. ..$ score : num 30
..$ 2:'data.frame': 1 obs. of 5 variables:
.. ..$ ID : num 13599
.. ..$ subject: num 2
.. ..$ gender : num 1
.. ..$ age : num 18
.. ..$ score : num 28
$ 14342:List of 2
..$ 1:'data.frame': 1 obs. of 5 variables:
.. ..$ ID : num 14342
.. ..$ subject: num 3
.. ..$ gender : num 1
.. ..$ age : num 28
.. ..$ score : num 27
..$ 2:'data.frame': 1 obs. of 5 variables:
.. ..$ ID : num 13370
.. ..$ subject: num 3
.. ..$ gender : num 1
.. ..$ age : num 28
.. ..$ score : num 32
I have absolutely no information on how to achieve this, and I don’t even know where to direct my research on this issue.
Playable Code:
myList1 <- list(
'1' = data.frame('ID' = c(13369,13599), 'subject' = c(2,2), 'gender' = c(1,1), 'age' = c(18,18), 'score' = c(30,28)),
'2' = data.frame('ID' = c(13370,14342), 'subject' = c(3,3), 'gender' = c(1,1), 'age' = c(28,28), 'score' = c(27,32))
)
Reproducible code for the result, if necessary:
myList2 <- list(
'13369' = list('1' = data.frame('ID' = 13369, 'subject' = 2, 'gender' = 1, 'age' = 18, 'score' = 30), '2' = data.frame('ID' = 13599, 'subject' = 2, 'gender' = 1, 'age' = 18, 'score' = 28)),
'13370' = list('1' = data.frame('ID' = 14342, 'subject' = 3, 'gender' = 1, 'age' = 28, 'score' = 27), '2' = data.frame('ID' = 13370, 'subject' = 3, 'gender' = 1, 'age' = 28, 'score' = 32)),
'13599' = list('1' = data.frame('ID' = 13369, 'subject' = 2, 'gender' = 1, 'age' = 18, 'score' = 30), '2' = data.frame('ID' = 13599, 'subject' = 2, 'gender' = 1, 'age' = 18, 'score' = 28)),
'14342' = list('1' = data.frame('ID' = 14342, 'subject' = 3, 'gender' = 1, 'age' = 28, 'score' = 27), '2' = data.frame('ID' = 13370, 'subject' = 3, 'gender' = 1, 'age' = 28, 'score' = 32))
)