"haven_labelled" format in crosstab

Hi,
I have imported from SPSS a dataframe with labelled variables.
When I use function class in R for some variable (i.e gender) I get format "haven_labelled".
In my report I am using some crosstabs and frequency tables (tidyverse + kable) but I want to show labels instead of values 1 for female and 2 for males.
I am having difficulties on this. I have seen recommendations about labelled library or expss but not been able to implement a steady solution.
Any ideas?
Regards

1 Like

If I understand your problem right, an easy solution would be coercing the vector into factor with haven::as_factor(), which sets the value labels as factor levels. Then, the table output automatically uses the labels.

Another option might be the frq()-function from the sjmisc-package. sjmisc primary goal is to complement dplyr by doing transformation tasks on variables (instead data frames, like dplyr), like recoding, dichotomizing or grouping variables and so on. All functions in sjmisc support labelled data.

Furthermore, the sjlabelled-package is useful in general when you work with labelled data. It does not directly provide outputs like frequency tables, but offers the underlying helper-function to deal with labelled data. It's very flexible, and the latest update also allows using quasiquotation (see vignette Using quasiquotation to add variable and value labels) to set / get value and varible labels.

Finally, the sjPlot-package has some functions that produces HTML-tables or ggplot2-plots, and also works with labelled data. sjt.xtab() creates crosstabs and uses value / variable labels from labelled data, and all table-output integrates well into knitr-(HTML-)documents.

P.S.: sorry, as a new user I can only use max. two links in a posting, so you need to look up the other webpages by yourself...

3 Likes

Thank you very much @strengejacke.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.