x no applicable method for 'tbl_vars' applied to an object of class "c('double', 'numeric')"

Viewed 4975

I am trying to count every id that corresponds to an unique id, however the next error keeps arising.

Here's my code:

direct_reports <- data.frame(df %>% 
                                              group_by(manager_id) %>% 
                                              summarise(number_dr = 
                                               count(manager_id)) %>% 
                                 select(manager_id, number_dr))

Console output:

Error: Problem with `summarise()` input `number_dr`.
x no applicable method for 'tbl_vars' applied to an object of class "c('double', 'numeric')"
i Input `number_dr` is `count(manager_id)`.
i The error occured in group 1: manager_id = 19292.
Run `rlang::last_error()` to see where the error occurred.

1 Answers

The issue is with count used inside the summarise. It would be n() instead of count

library(dplyr)
 df  %>%
      group_by(manager_id) %>% 
      summarise(number_dr = n(), .groups = 'drop')

There is no need to select after the group by summarise step as the columns remaining are only 'manager_id' and 'number_dr'


count expects a data.frame or tibble

df %>%
    count(manager_id)          
Related