sfirke/janitor

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

sjkiss opened this issue · 1 comments

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))

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.