R中的数据重塑

时间:2015-07-08 18:53:37

标签: r

我有以下数据

mydata <- data.frame(id=c(1,1,1,2,2,3,3,3,3,4,5,5,5), age=c(20,20,20,25,25,19,19,19,19,30,22,22,22), category=c("a","b","c","a","d","a","b","c","d","a","d","b","c"))

我想将其重塑为

ID  Age a   b   c   d
1   20  1   1   1   0
2   25  1   0   0   1
3   19  1   1   1   1
4   30  1   0   0   0
5   22  0   1   1   1

基本上我需要添加二进制列数='category'变量中的因子数。

1 个答案:

答案 0 :(得分:3)

您可以尝试dcast

 library(reshape2)
 dcast(mydata, id+age~category, value.var='category', length)
 #  id age a b c d
 #1  1  20 1 1 1 0
 #2  2  25 1 0 0 1
 #3  3  19 1 1 1 1
 #4  4  30 1 0 0 0
 #5  5  22 0 1 1 1

或使用dplyr/tidyr

 library(dplyr)
 library(tidyr)
 mydata %>%
       mutate(val=1) %>% 
       spread(category, val, fill=0)

@Pierre Lafortune建议的选项

 do.call(data.frame,aggregate(category~id+age, mydata, table))