Skip to content

milesdwilliams15/tabyl2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

tabyl2

A tabyl() command with group_by() Functionality

The tabyl() function in the janitor library is a simple method for obtaining a frequency table.

mtcars %>% tabyl(mpg) # percentage (really proportion) of cars with 'x' mpg.

The problem, however, is that tabyl() doesn't seem to work with group_by():

mtcars %>% group_by(cyl) %>% tabyl(mpg) # This yields the same results as the above example.

But, this problem can be solved by lifting the relevant count() and mutate() commands applied by the tabyl() function and by then using them directly (in the following order!):

mtcars %>% group_by(cyl) %>% count(mpg) %>% mutate(percent = n / sum(n, na.rm = TRUE))

However, this method requires some extra syntax. The whole point of the tabyl() function is to make life easier. So, with the end in mind of reducing how much typing we have to use in the future, I created the following function: tabyl2()

tabyl2 <- function (x, ..., wt = NULL, sort = FALSE) 
{
  vars <- lazyeval::lazy_dots(...)
  wt <- substitute(wt)
  count_(x, vars, wt, sort = sort) %>%
    mutate(percent = n/sum(n, na.rm=TRUE))
}

The guts are different compared to the inner workings of tabyl(), but the result is basically the same:

# Compare
mtcars %>% group_by(cyl) %>% tabyl2(mpg)
# to
mtcars %>% group_by(cyl) %>% count(mpg) %>% mutate(percent = n / sum(n, na.rm = TRUE))

About

A tabyl() command with group_by() Functionality

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages