1

There are numerous ways of counting the values in a vector, including the familiar (but fraught) table()

Is there a safe/reliable method that uses dplyr / tidyverse?

Note plyr::count() seems to work nicely, but is obviously from plyr rather than dplyr

c(1,3,3,3,4,4) %>% plyr::count()
  x freq
1 1    1
2 3    3
3 4    2
stevec
  • 41,291
  • 27
  • 223
  • 311

2 Answers2

2

dplyr functions are better suited for dataframe/tibbles than vectors. You can use dplyr::count after converting vector to tibble.

c(1,3,3,3,4,4) %>% tibble::as_tibble() %>% count(value)

#  value     n
#  <dbl> <int>
#1     1     1
#2     3     3
#3     4     2
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

We can also convert to data.frame

library(dplyr)
c(1,3,3,3,4,4) %>%
     data.frame(value = .) %>%
     count(value)

Or just use table

c(1,3,3,3,4,4) %>%
    table %>%
    as.data.frame
akrun
  • 874,273
  • 37
  • 540
  • 662