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

Allow for use of as_factor() for crosstabs of labelled variables #498

Closed
sjkiss opened this issue Dec 7, 2022 · 1 comment
Closed

Allow for use of as_factor() for crosstabs of labelled variables #498

sjkiss opened this issue Dec 7, 2022 · 1 comment

Comments

@sjkiss
Copy link

sjkiss commented Dec 7, 2022

It would be useful to allow janitor::tabyl() to wrap labelled variables in as_factor() to convert variables to factors on the fly.
There is a workaround, in the reprex below, but it would be nice to have the second example work just because it's such easy syntax.

library(labelled)
v <- labelled(sample(c(1,2), size=100, replace=T), c(yes = 1, no = 2))
x<-labelled(sample(c(1,2), size=100, replace=T), c(male = 1, female = 2))
df<-data.frame(v, x)
library(tidyverse)
library(janitor)
as_factor(df)
# This works
df %>% 
  as_factor() %>% 
  tabyl(., v, x)
# This does not but would be desirable
tabyl(df, as_factor(v), as_factor(x))
@sfirke
Copy link
Owner

sfirke commented Dec 8, 2022

I don't think that's possible, for the latter syntax. I'd like to do the same thing often with is.na() to check for missingness. But I first have to convert each variable with is.na() in a preceding call.

The only way I see would be to add an argument like factorize = T, but we try to avoid adding new arguments to functions and this is a very specialized case for users of the labelled package.

I will close this issue, but feel free to comment if there's something I'm missing.

@sfirke sfirke closed this as completed Dec 8, 2022
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