带有汇总r的逻辑值计数 [英] logical value count with summarise r

查看:199
本文介绍了带有汇总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
    )

这篇关于带有汇总r的逻辑值计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆