我有一个命名元素列表 (teSTList
),其中一些名称重复
$x
[1] "one"
$x
[1] "two"
$y
[1] "three"
$y
[1] "four"
我试图最终得到一个数据表,它将具有通用名称的元素组合到同一列中。
x y
1: one three
2: two four
我已经尝试过
testdf <- do.call(cbind, lapply(testlist, data.table))
但最终只会得到:
x.V1 x.V1 y.V1 y.V1
1: one two three four
有什么建议吗?感谢您的帮助!
最佳答案
尝试
library(data.table)#v1.9.5+
dcast(setDT(stack(testlist))[, N:= 1:.N, ind],
N~ind, value.var='values')[,N:=NULL][]
# x y
#1: one three
#2: two four
或者基本 R
方法是
unstack(stack(testlist),values~ind)
# x y
#1 one three
#2 two four
关于r - 将命名元素列表转换为数据框或数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31272379/