r - Nested list to data frame and back to nested list -
consider nested list data rectangular.
mylst1 <- list( "system" = list( "subjectid" = c(101,102,103), "procedureid" = c(202,202,203) ), "demographics" = list( "demo_age" = c(12,22,32), "demo_gender" = c(1,0,1) ), "items" = list( "n" = list( "n001" = c(1,2,3), "n002" = c(3,2,1) ), "e" = list( "e001" = c(1,2,3), "e002" = c(3,2,1) ) ) )
since nested lists awkward work with, let's create data frame: mydf <- data.frame(mylst1)
so far good, can perform operations on mydf
. let's assume filtered observations. problem need return same nested list structure required web application want send data to, data of course looks this:
> str(mydf) 'data.frame': 3 obs. of 8 variables: $ system.subjectid : num 101 102 103 $ system.procedureid : num 202 202 203 $ demographics.demo_age : num 12 22 32 $ demographics.demo_gender: num 1 0 1 $ items.n.n001 : num 1 2 3 $ items.n.n002 : num 3 2 1 $ items.e.e001 : num 1 2 3 $ items.e.e002 : num 3 2 1
what best solution data in original list format? thinking of using .
delimiter each level, i'm unsure how in practice.
thanks
one option relist
(assuming columns of same class
) in 'mydf'
newlst <- relist(unlist(mydf), skeleton = mylst1) identical(mylst1, newlst) #[1] true
Comments
Post a Comment