带有汇总r的逻辑值计数
问题描述:
在数据框中,我有一列具有Y和N值.该数据框还具有一个id列.我想创建两列,一列的每个ID的总Y计数,另一列的总N数.我尝试使用dplyr汇总功能执行此过程
In a data frame, I have a column with Y and N values. This data frame also has an id column. I would like to create two columns, one with the total Y count and another with the total N count for each id. I tried doing this procedure with the dplyr summarise function
group_by(id) %>%
summarise(total_not = count(column_y_e_n == "N"),
total_yes = count(column_y_e_n == "Y")
但反对错误消息
summarise_impl(.data,点)中的错误
Error in summarise_impl(.data, dots)
有任何建议吗?
答
Harro原始答案略有不同:
Slight variation on original answer from Harro:
library(tidyr)
dfr <- data.frame(
id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3),
bool = c("Y", "N", "Y", "Y", "Y", "Y", "N", "N", "N", "Y", "N", "N", "N")
)
dfrSummary <- dfr %>%
group_by(
id, bool
) %>%
summarize(
count = n()
) %>%
spread(
key = bool,
value = count,
fill = 0
)