0

Consider this code:

iris %>% count(Species) %>% group_by(Species)

# A tibble: 3 x 2
# Groups:   Species [3]
  Species        n
  <fct>      <int>
1 setosa        50
2 versicolor    50
3 virginica     50

I want to define a function which does the same task, something like this :

table_freq <- function(Table, Var) {

          freq <- NA
          freq <- Table %>%
                    dplyr::count(Var) %>%
                    group_by(Var)
          return(freq)
}
table_freq(iris, "Species")

But it does not work :

> table_freq(iris, "Species")
Error in `group_by_prepare()`:
! Must group by variables found in `.data`.
* Column `Var` is not found.

Any ideas?

Please do not write alternate solutions, I need to define a function that takes the table and the column name for which we need the freq. table.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    Does this answer your question? [Use dynamic name for new column/variable in \`dplyr\`](https://stackoverflow.com/questions/26003574/use-dynamic-name-for-new-column-variable-in-dplyr) – user438383 Apr 04 '22 at 16:16
  • NOt exactly this, but I found a new link thanks to it. Thank you ! – Wais Doualeh Apr 04 '22 at 16:25

2 Answers2

0

The secret sauce is {{}}, we simply write :

table_freq <- function(Table, Var) {

          freq <- NA
          freq <- Table %>%
                    dplyr::count({{Var}}) %>%
                    group_by({{Var}})
          return(freq)

}
table_freq(iris, Species)
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
0

You can use table to create a function that will take the table and the column name.

table_freq <- function(Table, Var) {
  setNames(data.frame(table(Table[,Var])), c(Var, "n"))
}

table_freq(iris, "Species")

Output

     Species  n
1     setosa 50
2 versicolor 50
3  virginica 50
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
  • 1
    That works as well, but I prefere using dplyr. Thanks ! – Wais Doualeh Apr 04 '22 at 18:23
  • @WaisDoualeh No worries, I typically use `dplyr` as well since I mostly work with small datasets. But, just FYI, if you ever have any really large datasets, then `table` will definitely be much quicker. – AndrewGB Apr 04 '22 at 18:32