Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR R

How to use group_by inside a function?

mytable <- function( x, group ) {
  x %>% 
    group_by( .data[[group]] ) %>% 
    summarise( n = n() )
}

group_string <- 'Species'

mytable( iris, group_string )

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
  Species        n
  <fct>      <int>
1 setosa        50
2 versicolor    50
3 virginica     50
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How
ADD COMMENT
Topic
Name
1+1 =