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

Other packages that would save me from doing this work? #2

Open
sfirke opened this issue Feb 3, 2017 · 7 comments
Open

Other packages that would save me from doing this work? #2

sfirke opened this issue Feb 3, 2017 · 7 comments

Comments

@sfirke
Copy link
Owner

sfirke commented Feb 3, 2017

questionr has no vignette so hard to tell. I like that it says it uses Shiny, but where can I see something about that? And functions like cprop don't work with data.frames, if that's true for the underlying data classes and other functions this may not be tidyverse-friendly.

memisc?

Could try surveydata but the limitations noted on its README seem undesirable. I think better to not invent a new class of object, and instead work around limitations of data.frames if possible.

@sfirke
Copy link
Owner Author

sfirke commented Feb 28, 2017

Ooh maybe the labelled package has the right idea: use the labelled class from haven. Here's the vignette.

@sfirke
Copy link
Owner Author

sfirke commented Feb 28, 2017

I wrote an S3 method for tabyl on a labelled vector in a normal data.frame.

tabyl.labelled <- function (vec, sort = FALSE, show_na = TRUE, ...) {
  var_text <- labelled::var_label(vec)
  vec <- to_factor(vec)
  res <- janitor::tabyl(vec, sort, show_na, ...)
  names(res)[1] <- var_text
  res
}

That drops the var_label into the 1st name of the tabyl output:

> tabyl(sav_responses$q0001)
               What is your current role on your campus?   n     percent
1              Other, not administrator (please specify)  13 0.020280811
2                                              Principal 145 0.226209048
3                                    Assistant Principal 223 0.347893916
4                                                   Dean  41 0.063962559
5                                                  Coach 214 0.333853354
6                                   Administrator, other   0 0.000000000
7                        Classroom teacher (not a coach)   5 0.007800312
8        I do not currently work in XXXXX Public Schools   0 0.000000000

The annoyance is that actually, a column can have a var_label without being class labelled. So an S3 method doesn't work. Would there be harm in coercing all variables in a data.frame to be labelled, for this purpose? Could there be a leaner class that just has var_label attribute, and we add that as a class to every var_label'ed column in the data.frame?

@sfirke
Copy link
Owner Author

sfirke commented Jun 6, 2017

Maybe some mix of Hmisc and labelled packages. Hmisc::label() does set class = labelled when called which I need for S3. But I like the interface of the labelled package better.

So maybe the data storage paradigm is, labels in the label attribute, then create tabyl, crosstab, and check-all-that-apply S3 methods. Value labels might be trickier, but they could pull the var label and set it as the column name before calling the regular tabyl.data.frame.

W/ the current SurveyMonkey process, it would be nice to set all labels at once. I can't find a function that lets me supply a character vector to a data.frame, unbelievably. I guess I'll wrap my StackOverflow answer into a function?

@sfirke
Copy link
Owner Author

sfirke commented Jun 6, 2017

I could use this approach to create a named list, then pass that to labelled::var_labels - but I don't see how that's any better than my for loop linked on StackOverflow above.

@sfirke
Copy link
Owner Author

sfirke commented Jan 26, 2018

There's also now the labelVector package: https://cran.r-project.org/web/packages/labelVector/vignettes/labelVector.html. Designed for use in other packages, and labelVector::set_label does set class to "labelled"!

@sfirke
Copy link
Owner Author

sfirke commented Jan 26, 2018

There is also sjlabelled on CRAN. Gah.

@sfirke
Copy link
Owner Author

sfirke commented Feb 13, 2019

Update: now, using janitor v1+ and setting labels with labels(dat) <- char_vec using papeR::labels, the functional prototype is:

tabyl.lv <- function (vec, show_na = TRUE, ...) {
  var_text <- attr(vec, "variable.label")
  vec <- labelled::to_factor(vec)
  res <- janitor::tabyl(vec, show_na, ...)
  names(res)[1] <- var_text
  res
}

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

1 participant