dataframe - convert row in data frame to list of lists in r -


i have data frame consisting of records following. typical row of data frame, df[1,] looks follows

84745,"f",70,7,"single",2,"n",4,9,1,1,3,4,4,"2 day","<120 , <80",0,8,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1 

i want convert variable myvar below of following type

myvar = list( list(84745,"f",70,7,"single",2,"n",4,9,1,1,3,4,4,"2 day","<120 , <80",0,8,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1)) 

i have tried doing following, doesn't work convert list of lists.

myvar <- as.list(as.list(as.data.frame(t(df[1,])))) 

how can that?

edit : have tried myvar = list(unclass(df[14,])). fails call since formatting of output myvar different.

format of original line of code

[[1]] [[1]][[1]] [1] 21408  [[1]][[2]] [1] "m"  [[1]][[3]] [1] 69  [[1]][[4]] [1] 3  [[1]][[5]] [1] "widowed" 

format of myvar = list(unclass(df[14,]))

[[1]] [[1]]$id [1] "21408"  [[1]]$gendercd [1] "m"  [[1]]$age [1] "69"  [[1]]$los [1] "3"  [[1]]$maritalstatus [1] "widowed" 

try this:

 myvar <- list( unclass( df[1,] ) 

explanation: df[1,] still list "data.frame" class attribute. if remove class it's ordinary list. when conducted t(df[1,]-operation forced row become column vector in dataframe needed came class coercion occurred.

if goal row-by-row solution this:

 myvar <- list()  (i in seq(nrow(df)) ) { myvar[[i]]  <-  unclass( df[i,] )} 

if needs unnamed rather doubt suppose it's possible then:

 myvar <- list()  (i in seq(nrow(df)) ) { myvar[[i]]  <-  unname( unclass( df[i,] )) } 

i tested unname strategy with:

> unname(unclass( data.frame(a=345,b="tyt")[1,])) [[1]] [1] 345  [[2]] [1] tyt levels: tyt  attr(,"row.names") [1] 1 

Comments