I suppose this is simple, but I just can't seem to figure it out.
I need to flatten the second level structure and push the list name/key to a vector on the same level as the other vectors. The current structure of myList
is
$ 13454:List of 30
..$ subjectId : num 187
..$ procedureId : num 3
..$ procedureSampleId: num 3
..$ timestamp : chr "2017-04-21T17:15:10.911Z"
..$ n001 : num -999
..$ n002 : num -999
..$ gender : num 1
..$ age : num 18
$ 13455:List of 30
..$ subjectId : num 188
..$ procedureId : num 3
..$ procedureSampleId: num 3
..$ timestamp : chr "2017-04-21T17:15:10.913Z"
..$ n001 : num -999
..$ n002 : num -999
..$ gender : num -999
..$ age : num 28
whereas this is the structure I'm looking for
$ ID : chr '13455' '13455'
$ subjectId : num 187 188
$ procedureId: : num 3 3
and so on
I've tried to achieve this by:
myList2 <- sapply(names(myList), function(y){
y <- unlist(c('ID' = y, myList[[y]]), use.names = TRUE)
})
But I end up with the full transposed result of what I need. I could go t(myList2)
but I want to understand how to do this correctly. Thank you!
EDIT: Reproducible data:
myList <- list('13454' = list('subjectId' = 187, 'procedureId' = 3, 'procedureSampleId' = 3, 'timestamp' = "2017-04-21T17:15:10.911Z", 'n001' = -999, 'n002' = -999, 'gender' = 1, 'age' = 18), '13455' = list('subjectId' = 188, 'procedureId' = 3, 'procedureSampleId' = 3, 'timestamp' = "2017-04-21T17:15:10.913Z", 'n001' = -999, 'n002' = -999, 'gender' = -999, 'age' = 28))