R reshape2 dcast: transform data

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

How can I transform data X to Y as in

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 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.

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?

回答1:

According to the documentation (?reshape2::dcast), dcast() allows for ... in the formula:

"..." represents all other variables not used in the formula ...

This is true for both the reshape2 and the data.table packages which both support dcast().

So, you can write:

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 

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 

which is in line with the expected answer given as data.frame Y.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!