Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use tabyl with group_by? #200

Closed
randomgambit opened this issue May 10, 2018 · 2 comments
Closed

use tabyl with group_by? #200

randomgambit opened this issue May 10, 2018 · 2 comments

Comments

@randomgambit
Copy link

Hello @sfirke,

Thanks for this very nice package.

I am confused as to whether tabyl can be used with a group_by.

Here is the common usage: group_by some variable and print the output of tabyl(myvar) for each group.

I was surprisingly unable to do so with a naive group_by(mygroup) %>% tabyl(myvar)

Can I do that with tabyl? is it meant to be used with grouping?
Thanks!

@sfirke
Copy link
Owner

sfirke commented May 10, 2018

Hi! tabyl ungroups a grouped input, treating it as if it was ungrouped: #125

If you want to group by one variable and count another, you simply do a two-way tabyl:

> mtcars %>% tabyl(am, cyl)
 am 4 6  8
  0 3 4 12
  1 8 3  2

Then calculate your percentages etc. with adorn_ functions.

In terms of the numbers you get, I think you can produce the same #s from a 1-way tabyl in this 2-way tabyl. I'd say this is the "janitor-ish" solution.

But if you preferred the style of a list of 1-way tabyls, you could try:

> split(mtcars, mtcars$cyl) %>%
+   purrr::map(tabyl, am)
$`4`
 am n   percent
  0 3 0.2727273
  1 8 0.7272727

$`6`
 am n   percent
  0 4 0.5714286
  1 3 0.4285714

$`8`
 am  n   percent
  0 12 0.8571429
  1  2 0.1428571

I gather there are more tidyverse-oriented alternatives to split, say nesting from purrr or this proposed chop: https://coolbutuseless.bitbucket.io/2018/03/07/chop-a-tidyverse-style-split/

@randomgambit
Copy link
Author

Hi, very interesting indeed. No I do now want to use a double sort here because I have quite a bit of different groups (so as you correctly guess I need to keep a long format here).

The purrr solution seems fine, although I wonder if janitor could/should handle that natively, by binding together the by-group dataframes shown in the purrr output

@sfirke sfirke closed this as completed Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants