r - Converting a list of named elements into a data frame or data table -
i have list of named elements (testlist) of names duplicated
$x [1] "one" $x [1] "two" $y [1] "three" $y [1] "four" and trying end data table combine elements common names same column.
x y 1: 1 3 2: 2 4 i have tried
testdf <- do.call(cbind, lapply(testlist, data.table)) but end with:
x.v1 x.v1 y.v1 y.v1 1: 1 2 3 4 any suggestions? appreciate help!
try
library(data.table)#v1.9.5+ dcast(setdt(stack(testlist))[, n:= 1:.n, ind], n~ind, value.var='values')[,n:=null][] # x y #1: 1 3 #2: 2 4 or base r approach be
unstack(stack(testlist),values~ind) # x y #1 1 3 #2 2 4
Comments
Post a Comment