-
Notifications
You must be signed in to change notification settings - Fork 130
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
Integration with labelled
package set_value_labels()
+ haven_labelled
class
#488
Comments
Hello @calebasaraba ! Thank you for the note!! I will need to put more thought into whether or not to extend gtsummary to accept other classes. The the case of haven labelled, it was never meant to be a class that was used in analysis or data exploration. Rather, it was created as an in-between when importing data from other languages where the data types don't have a one-to-one relationship with R. This is from a tidyverse blogpost about the haven labelled class of variables. (https://haven.tidyverse.org/articles/semantics.html)
For the time being, I recommend you convert the variables to factor with Happy Coding! |
Got it, thanks for the clarification about intended use of the I have been enjoying the way Thanks for your quick response and all your awesome work :) |
For now, we are going to add more specific messaging around |
Just a quick comment, labelled vectors are not always intended to be converted into factors. For example, you could have an age variable and add a label to value 99 to say that 99 represent "99 or more". This is why it is the responsability of the user to unclass or to convert into a factor, depending on the fact that the variable should be treated as continuous or categorical. A quick type is to use
But these hypothesis works only if the users have documented properly the vectors. More details on https://larmarange.github.io/labelled/articles/intro_labelled.html#conditionnal-conversion-to-factors-1 |
Im having a similar issue |
@muminbayoumi can you post an example I can run on my machine? Aka A reprex |
I'll have to apologise - seems the base issue is with base R and using droplevels function. However this reprex illustrates how the factor levels which empty are still printed . Are you planning on adding an option to exclude those? library(expss)
library(tidyverse)
library(forcats)
library(gtsummary)
library(sjmisc)
data <- tibble(.rows = 200)
data$CatColumAsFactor <- as_factor(sample(c('Apple','Banana','Cherry'),200,replace = T))
data$CatColumAsCharacter <- sample(c('Apple','Banana','Cherry'),200,replace = T)
data <- apply_labels(data,
CatColumAsCharacter='Character Column',
CatColumAsFactor= 'Factor Column')
## Without dropping filtered levels all levels printed on factor column
## Only levels with values printed on character column
data %>% filter(CatColumAsFactor!='Cherry',CatColumAsCharacter!='Apple')%>%
tbl_summary()
## On dropping levels
##Label attribute lost and therefore not picked up by gtsummary
data %>% filter(CatColumAsFactor!='Cherry',CatColumAsCharacter!='Apple') %>% droplevels() %>%
tbl_summary()
## to_label preserves the labels
data %>% filter(CatColumAsFactor!='Cherry',) %>% sjmisc::to_label(drop.levels=T) %>%
tbl_summary()
The tables aren't rendering very well with reprex() function - so I took them out. |
Thank you for showing me this package! I hadn't heard of exprss before, and it's such a popular package! @muminbayoumi Showing the unobserved factors is a feature I think is useful. If you want unobserved factors removed, you can remove the levels before passing the data frame to data %>%
filter(CatColumAsFactor != 'Cherry', CatColumAsCharacter != 'Apple') %>%
mutate_if(is.factor, factor) %>% # removes unobserved levels
tbl_summary() |
Amazing package -- really love this project. I am trying to use it alongside with the
labelled
package and when using theset_value_labels()
function I get an error:Not sure if this intentional behavior for the package, or if it would be an easy fix. Using factor labels for the variable levels (like below) works, but it would be great if
gtsummary()
would also accept thehaven_labelled
class, as I'm seeing it used more and more.The text was updated successfully, but these errors were encountered: