Search code examples
rdplyrgroupingsummarization

logical value count with summarise r


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")

but objected to the error message

Error in summarise_impl(.data, dots)

Any sugestion?


Solution

  • 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
        )