我有一个数据集D的最小示例,看起来像这样:
score person freq
10 1 3
10 2 5
10 3 4
8 1 3
7 2 2
6 4 1
现在,我希望能够根据人员绘制得分为10的频率图,但是如果我这样做:
#My bad, turns out the next line only works for matrices anyway:
#D = D[which(D[,1] == 10)]
D = subset(D, score == 10)
然后我获得:
score person freq
10 1 3
10 2 5
10 3 4
然而,这是我想要得到的:
但是,这是我希望获得的:
score person freq
10 1 3
10 2 5
10 3 4
10 4 0
有没有一种在R中快速且不费力的方法来做这件事?
complete(df, score, person, fill = list(freq = 0)) %>% filter(score == 10)
- talat