tidyr ::传播但保留原始数据

时间:2019-06-06 09:05:31

标签: r tidyr spread

数据

test = data.frame(case=c("cyl","eng","mon"),
worst=c(1,0,1),money=c(123,42,13),girl=c("no","yes","no"))

enter image description here

desired_test= data.frame(case=c("cyl","eng","mon"), worst=c(1,0,1),money=c(123,42,13),girl=c("no","yes","no"),
                       worst_cyl=c(1,0,0),worst_eng=c(0,0,0),worst_mon=c(0,0,1))

enter image description here

尝试:

test %>% group_by(case) %>% spread(case,worst,fill=0,sep="_")

为什么我的尝试无效?

1 个答案:

答案 0 :(得分:3)

我假设这在较大的数据集的上下文中更有意义,但是您可以通过添加rowid_to_column并绑定spread中使用的键/值列来获得所需的结果:

library(tidyr)
library(dplyr)

test %>%
  rowid_to_column() %>%
  spread(case,worst,fill=0,sep="_") %>%
  cbind(test[c("case", "worst")], .) %>%
  rename_at(vars(starts_with("case_")), ~sub("case", "worst", .x)) %>%
  select(-rowid)

  case worst money girl worst_cyl worst_eng worst_mon
1  cyl     1   123   no         1         0         0
2  eng     0    42  yes         0         0         0
3  mon     1    13   no         0         0         1