【发布时间】:2016-07-24 20:04:15
【问题描述】:
很抱歉,这个问题已被问几次次,但我仍然无法解决这个问题。
所以我有一个数据框,格式为:
ID Val Type
1 10 A
2 11 A
2 10 C
3 10 B
3 12 C
4 9 B
帮助不大,但你可以使用
library(tidyr)
test <- data.frame(ID = c(1,2,2,3,3,4),
Val = c(10,11,10,10,12,9),
Type = c('A', 'A', 'C', 'B', 'C', 'B'))
我想拆分得到:
ID A.Type B.Type C.Type A.Val B.Val C.Val
1 1 0 0 10 0 0
2 1 0 1 11 0 10
3 0 1 1 0 10 12
4 0 0 0 0 9 0
我知道如何获取列 1:4 使用:
table(test[, c(1, 3)]) %>% as.data.frame() %>% spread(Type, Freq)
这是我需要帮助的最后三个,因为在实际数据帧中值是连续的,table 不能使用。
【问题讨论】:
-
spread(test, Type, Val)?