【发布时间】:2020-02-28 05:07:56
【问题描述】:
我有一个这样的数据集:
Year Month Day Location Target Perpetrator
1970 5 1 Place1 x A
1970 7 5 Place2 y A
1971 2 3 Place3 x B
1972 10 8 Place4 x C
1972 12 13 Place2 y C
1973 1 3 Place5 z B
我完全不知道如何做到这一点。我试过了
data <- data %>%
distinct() %>%
count(Perpetrator)
但这当然只给了我“犯罪者”中每个唯一值的计数。
输出 I 是按年份计算的“犯罪者”中每个唯一值的计数。我该怎么做?
【问题讨论】:
-
试试
data %>% group_by(Year) %>% distinct() %>% count(perpetrator) -
这正是我想要的!我已经尝试了几个小时,非常感谢
-
另一种方式(不会导致 tibble)是来自
plyr包的ddply(data, .(Year), summarise, n = n_distinct(Perpetrator))。我个人更喜欢这种方式,因为我讨厌 tibbles:P -
可以
count多个变量data %>% count(Year, Perpetrator)