R reshape2 dcast:转换数据 [英] R reshape2 dcast: transform data
问题描述
如何像这样
X = data.frame(
ID = c(1,1,1,2,2),
NAME = c("MIKE","MIKE","MIKE","LUCY","LUCY"),
SEX = c("MALE","MALE","MALE","FEMALE","FEMALE"),
TEST = c(1,2,3,1,2),
SCORE = c(70,80,90,65,75)
)
Y = data.frame(
ID = c(1,2),
NAME = c("MIKE","LUCY"),
SEX = c("MALE","FEMALE"),
TEST_1 =c(70,65),
TEST_2 =c(80,75),
TEST_3 =c(90,NA)
)
The <$ c $ reshape2
中的c> dcast 函数似乎可以正常工作,但是在上面的示例中它不能在数据中包含其他列,例如ID,NAME和SEX。
The dcast
function in reshape2
seems to work but it can not include other columns in the data like ID, NAME and SEX in the example above.
假设ID列的所有其他列都一致,例如Mike只能是ID为1的男性,我们该怎么做?
Assuming all other columns by a ID column are consistent, like Mike can only be a male with ID 1, how can we do it?
推荐答案
根据文档(?reshape2 :: dcast
), dcast ()
在公式中允许 ...
:
According to the documentation (?reshape2::dcast
), dcast()
allows for ...
in the formula:
...表示公式中未使用的所有其他变量...
"..." represents all other variables not used in the formula ...
对于同时支持 dcast()的
。 reshape2
和 data.table
包都是如此。 )
This is true for both the reshape2
and the data.table
packages which both support dcast()
.
因此,您可以编写:
reshape2::dcast(X, ... ~ TEST, value.var = "SCORE")
# ID NAME SEX 1 2 3
#1 1 MIKE MALE 70 80 90
#2 2 LUCY FEMALE 65 75 NA
但是,如果OP坚持认为列名称应为 TEST_1
, TEST_2
等,在重塑之前需要修改 TEST
列。在这里,使用 data.table
:
However, if the OP insists that the column names should be TEST_1
, TEST_2
, etc., the TEST
column needs to be modified before reshaping. Here, data.table
is used:
library(data.table)
dcast(setDT(X)[, TEST := paste0("TEST_", TEST)], ... ~ TEST, value.var = "SCORE")
# ID NAME SEX TEST_1 TEST_2 TEST_3
#1: 1 MIKE MALE 70 80 90
#2: 2 LUCY FEMALE 65 75 NA
与预期的答案一致data.frame Y
。
which is in line with the expected answer given as data.frame Y
.
这篇关于R reshape2 dcast:转换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!