我的数据框有两列,date
和id
> test
date id
1 2018-01-01 a
2 2018-01-02 b
3 2018-01-03 c
4 2018-01-04 d
5 2018-01-01 e
6 2018-01-02 f
7 2018-01-03 a
8 2018-01-04 b
9 2018-01-01 c
10 2018-01-01 d
11 2018-01-01 d
我的目标是将其整形为一个表,该表计算每个日期中每个不同id
(在数据集中)每次出现的次数,而如果没有出现id
则保留0使用dplyr
或data.table
所需的结果输出如下所示:
> output
date a b c d e f
1 2018-01-01 1 0 1 2 1 0
2 2018-01-02 0 1 0 0 0 1
3 2018-01-03 1 0 1 0 0 0
4 2018-01-04 0 1 0 1 0 0
此代码不会产生所需的结果:
test %>%
group_by(date, id) %>%
summarise_all(funs(sum))
使用dplyr
或data.table
生成所需输出的正确方法是什么?
以下是样本数据的副本:
> dput(test)
structure(list(date = structure(c(17532, 17533, 17534, 17535,
17532, 17533, 17534, 17535, 17532, 17532, 17532), class = "Date"),
id = c("a", "b", "c", "d", "e", "f", "a", "b", "c", "d",
"d")), .Names = c("date", "id"), row.names = c(NA, 11L), class = "data.frame")