将命名元素的列表转换为数据帧或数据表 [英] Converting a list of named elements into a data frame or data table
本文介绍了将命名元素的列表转换为数据帧或数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些名称元素的列表( testlist
),其中一些名称被复制
I have a list of named elements (testlist
) where some of the names are duplicated
$x
[1] "one"
$x
[1] "two"
$y
[1] "three"
$y
[1] "four"
我试图找到一个数据表,它将通用名称的元素组合到同一列中。
And I am trying to end up with a data table that will combine the elements with common names into the same column.
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
或 base R
方法将是
unstack(stack(testlist),values~ind)
# x y
#1 one three
#2 two four
这篇关于将命名元素的列表转换为数据帧或数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文