Dplyr equivalent of plyr functions

Hi All,
What is the dplyr's equivalent of plyr's count () function ?
I want to calculate simply an occurences of a values in a column of dataframe using dplyr.

library(plyr)

df3 <- structure(list(Var1 = structure(1:15, .Label = c("4", "5", "23", 
"34", "43", "54", "56", "65", "67", "324", "435", "453", "456", 
"567", "657"), class = "factor"), Var2 = c(2L, 1L, 2L, 2L, 1L, 
1L, 2L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-15L))

plyr::count(df3$Var2)
#>   x freq
#> 1 1    9
#> 2 2    5
#> 3 3    1

Created on 2020-04-11 by the reprex package (v0.3.0)

dplyr also has a count function.

sample_data <- structure(.Data = list(Var1 = structure(.Data = 1:15,
                                                       .Label = c("4", "5", "23", "34", "43", "54", "56", "65", "67", "324", "435", "453", "456", "567", "657"),
                                                       class = "factor"),
                                      Var2 = c(2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L)),
                         class = "data.frame",
                         row.names = c(NA, -15L))

plyr::count(df = sample_data,
            vars = "Var2")
#>   Var2 freq
#> 1    1    9
#> 2    2    5
#> 3    3    1

dplyr::count(x = sample_data,
             Var2)
#> # A tibble: 3 x 2
#>    Var2     n
#>   <int> <int>
#> 1     1     9
#> 2     2     5
#> 3     3     1

Hope this helps.


Edit (in reply to the following post #3)

Have you noticed the way I used it? The dplyr version takes a tbl as the first argument, and then the variables to count on.

Hi and thank you @Yarnabrina,
I tried:

dplyr::count(df3$Var2)

before, but it gave an error:

While I am learning dplyr I still do like a plyr.
I have found this:
https://jimhester.github.io/plyrToDplyr/

If you All have got any similar examples (dplyr equivalents of plyr functions) please share here.
Thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.