tidyr::spread 但保留原始数据
tidyr::spread but keep original data
数据:
test = data.frame(case=c("cyl","eng","mon"),
worst=c(1,0,1),money=c(123,42,13),girl=c("no","yes","no"))
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))
尝试:
test %>% group_by(case) %>% spread(case,worst,fill=0,sep="_")
为什么我的尝试不起作用?
我假设这在更大数据集的上下文中更有意义,但您可以通过添加 rowid_to_column
并绑定 spread
中使用的 key/value 列来实现所需的结果:
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
数据:
test = data.frame(case=c("cyl","eng","mon"),
worst=c(1,0,1),money=c(123,42,13),girl=c("no","yes","no"))
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))
尝试:
test %>% group_by(case) %>% spread(case,worst,fill=0,sep="_")
为什么我的尝试不起作用?
我假设这在更大数据集的上下文中更有意义,但您可以通过添加 rowid_to_column
并绑定 spread
中使用的 key/value 列来实现所需的结果:
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