我有一张这样的table:
data1 <- data.frame("State" = c("NJ", "NJ", "PA", "NJ", "TX"), "Filter" = c("Filter", "Filter",
"No Filter", "Filter", "Filter"), "Threshold" = c("Exceeds","Exceeds", NA, "NL", "Exceeds"))
字符串
我想创建一个计数表,按状态和过滤器分组,然后计算阈值为“超出”的次数,并将该值放入新列中。然后计算某个State和Filter组合出现的次数,并将其放在另一列中。下面是我正在寻找的一个例子。
final_data <- data.frame("State" = c("NJ", "NJ", "PA", "NJ", "TX"), "Filter" = c("Filter",
"Filter", "No Filter", "Filter", "Filter"), "Threshold" = c("Exceeds", "Exceeds", NA, "NL",
"Exceeds"), Count_Exceeds_Threshold = c(2, 2, 0, 0, 1), Count_Total = c(3, 3, 1, 3, 1))
型
我已经尝试在dplyr中使用group_by和tally()来解决这个问题,但是我不能让它以我想要的方式工作。
谢谢你!
2条答案
按热度按时间h79rfbju1#
您可以将
add_count()
用于以下两种目的:字符串
iecba09b2#
您可以使用
mutate
和.by
进行内联分组,并计算Threshold == "Exceeds"
的次数。n()
用于按组获取行数。字符串