R reshape2 dcast:转换数据

时间:2016-11-03 08:57:31

标签: r reshape2 dcast

如何将数据X转换为Y,如

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

dcast中的reshape2函数似乎有效,但上述示例中的ID,NAME和SEX等数据中不能包含其他列。

假设ID列的所有其他列都是一致的,就像Mike只能是ID为1的男性一样,我们该怎么办呢?

1 个答案:

答案 0 :(得分:1)

根据文档(?reshape2::dcast),dcast()允许公式中的...

  

" ..."表示公式中未使用的所有其他变量...

对于同时支持reshape2的{​​{1}}和data.table个包都是如此。

所以,你可以写:

dcast()

但是,如果OP坚持列名称应为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 TEST_1等,则需要在重新整形之前修改TEST_2列。在这里,使用了TEST

data.table

与data.frame 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 给出的预期答案一致。