0

I'm having trouble running dplyr::tally on factors and characters

> data %>% dplyr::tally(true_label)
Error: Problem with `summarise()` column `n`.
ℹ `n = sum(true_label, na.rm = TRUE)`.
x ‘sum’ not meaningful for factors
Run `rlang::last_error()` to see where the error occurred.

> rlang::last_error()
<error/dplyr_error>
Problem with `summarise()` column `n`.
ℹ `n = sum(true_label, na.rm = TRUE)`.
x ‘sum’ not meaningful for factors
Backtrace:
  1. data %>% dplyr::tally(true_label)
 12. base::.handleSimpleError(...)
 13. dplyr:::h(simpleError(msg, call))
Run `rlang::last_trace()` to see the full context


> data %>% dplyr::tally(as.character(true_label))
Error: Problem with `summarise()` column `n`.
ℹ `n = sum(as.character(true_label), na.rm = TRUE)`.
x invalid 'type' (character) of argument
Run `rlang::last_error()` to see where the error occurred.


> rlang::last_error()
<error/dplyr_error>
Problem with `summarise()` column `n`.
ℹ `n = sum(as.character(true_label), na.rm = TRUE)`.
x invalid 'type' (character) of argument
Backtrace:
  1. data %>% dplyr::tally(as.character(true_label))
 10. base::.handleSimpleError(...)
 11. dplyr:::h(simpleError(msg, call))
Run `rlang::last_trace()` to see the full context.

This also fails:

> data %>% group_by(true_label) %>% summarise(n_label = n())
Error: `n()` must only be used inside dplyr verbs.
Run `rlang::last_error()` to see where the error occurred.

Alternative examples:

diamonds %>% tally(cut)

diamonds %>% group_by(as.factor(cut)) %>% summarise(n_label = n())

I believe summarise is a dplyr verb, isn't it?

What is the official way to count factors and characters?

dplyr version 1.0.7

Eric Leung
  • 2,354
  • 12
  • 25
Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Is `data` a data.frame or tibble? Or just a character vector? What packages do you currently have loaded? Maybe you have loaded a package that redefined `summarise` – MrFlick Aug 06 '21 at 18:26
  • 1
    `diamonds %>% group_by(as.factor(cut)) %>% summarise(n_label = n())` works fine for me without any error. Do you have `plyr` loaded? Try using `dplyr::summarise`. Also you can do `dplyr::count(diamonds, cut)` – Ronak Shah Aug 07 '21 at 13:55

1 Answers1

0

You don't need to pass any additional arguments to tally(). You just need the group_by() using the categorical column to count on.

library(dplyr)

iris %>%
  group_by(Species) %>%
  tally()
#> # A tibble: 3 x 2
#>   Species        n
#>   <fct>      <int>
#> 1 setosa        50
#> 2 versicolor    50
#> 3 virginica     50

Created on 2021-08-06 by the reprex package (v2.0.0)

Eric Leung
  • 2,354
  • 12
  • 25