使用data.table功能将长结构化的data.table重塑为广泛的结构吗? [英] Reshape long structured data.table into a wide structure using data.table functionality?
问题描述
> library(data.table)
> A <- data.table(x = c(1,1,2,2), y = c(1,2,1,2), v = c(0.1,0.2,0.3,0.4))
> A
x y v
1: 1 1 0.1
2: 1 2 0.2
3: 2 1 0.3
4: 2 2 0.4
> B <- dcast(A, x~y)
Using v as value column: use value.var to override.
> B
x 1 2
1 1 0.1 0.2
2 2 0.3 0.4
很明显,我可以使用fx将data.table从长整形改成宽整形dcast软件包reshape2。但是data.table附带了一个重载的括号运算符,提供了 by和 group之类的参数,这让我想知道是否可以使用它来实现(针对data.table的特定功能)?
Apparently I can reshape a data.table from long to wide using f.x. dcast of package reshape2. But data.table comes along with an overloaded bracket-operator offering parameters like 'by' and 'group', which make me wonder if it is possible to achieve it using this (to data.table specific functionality)?
这是手册中的一个随机示例:
Just one random example from the manual:
DT[,lapply(.SD,sum),by=x]
这看起来很棒-但我不喜欢
That looks awesome - but I don't fully understand the usage yet.
我既没有找到方法也没有例子,所以也许是不可能的,甚至是不应该的-因此,当然,一个肯定的不,因为……是不可能的也是一个有效的答案。
I neither found a way nor an example for this so maybe it is just not possible maybe it isn't even supposed to be - so, a definite "no, is not possible because ..." is then of course also a valid answer.
推荐答案
我会选择一个具有不相等组的示例,以便在一般情况下更易于说明:
I'll pick an example with unequal groups so that it's easier to illustrate for the general case:
A <- data.table(x=c(1,1,1,2,2), y=c(1,2,3,1,2), v=(1:5)/5)
> A
x y v
1: 1 1 0.2
2: 1 2 0.4
3: 1 3 0.6
4: 2 1 0.8
5: 2 2 1.0
第一步是获取每个 x组的元素/条目数相同。在这里,对于x = 1,存在3个y值,但是对于x = 2,只有2个值。因此,我们必须首先用NA来解决x = 2,y = 3。
The first step is to get the number of elements/entries for each group of "x" to be the same. Here, for x=1 there are 3 values of y, but only 2 for x=2. So, we'll have to fix that first with NA for x=2, y=3.
setkey(A, x, y)
A[CJ(unique(x), unique(y))]
现在,要使其具有较宽的格式,我们应该按 x分组,并在 v
上使用 as.list
如下:
Now, to get it to wide format, we should group by "x" and use as.list
on v
as follows:
out <- A[CJ(unique(x), unique(y))][, as.list(v), by=x]
x V1 V2 V3
1: 1 0.2 0.4 0.6
2: 2 0.8 1.0 NA
现在,您可以使用带有 setnames
的引用来设置重塑列的名称,如下所示:
Now, you can set the names of the reshaped columns using reference with setnames
as follows:
setnames(out, c("x", as.character(unique(A$y)))
x 1 2 3
1: 1 0.2 0.4 0.6
2: 2 0.8 1.0 NA
这篇关于使用data.table功能将长结构化的data.table重塑为广泛的结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!