-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
case_when
within mutate
loses variable labels
#6857
Comments
sjlabelled just adds a label attribute onto a double or integer vector. That isn't enough for our underlying infrastructure to be able to figure out that that information is important. It needs to use a real class like library(haven)
library(dplyr)
x <- labelled(c(1, 2, 1, 3, 2), label = "response type")
x
#> <labelled<double>[5]>: response type
#> [1] 1 2 1 3 2
# Doesn't ever "see" `x`. It gets a logical vector and `50`
case_when(x == 1 ~ 50)
#> [1] 50 NA 50 NA NA
# Explicitly tell `case_when()` to use `x` as the type
case_when(x == 1 ~ 50, .ptype = x)
#> <labelled<double>[5]>: response type
#> [1] 50 NA 50 NA NA
# Or supply `x` as the default if you just want to replace part of it
case_when(x == 1 ~ 50, .default = x)
#> <labelled<double>[5]>: response type
#> [1] 50 2 50 3 2
# Similar thing here
case_match(x, 1 ~ 50, .default = x)
#> <labelled<double>[5]>: response type
#> [1] 50 2 50 3 2 So I'd suggest switching to haven if you can. Or possibly manually converting the sjlabelled results over to haven. |
I see, thank you. You will notice that I did not use suppressWarnings(suppressPackageStartupMessages(library(qualtRics)))
suppressWarnings(suppressPackageStartupMessages(library(sjlabelled)))
suppressWarnings(suppressPackageStartupMessages(library(haven)))
# Extract all surveys
surveys <- all_surveys()
# # Identify right survey
survey1.id <- surveys$id[
which("Projet priming-aggression (Part 1)_Study 3" == surveys$name)]
# # Fetch right survey
data <- suppressMessages(fetch_survey(surveyID = survey1.id, verbose = FALSE))
# sjlabelled works
get_label(data$Status)
#> Status
#> "Response Type"
# haven doesn't work
print_labels(data$Status)
#> Error in `print_labels()`:
#> ! `x` must be a labelled vector.
#> Backtrace:
#> ▆
#> 1. └─haven::print_labels(data$Status)
#> 2. └─cli::cli_abort("{.arg x} must be a labelled vector.")
#> 3. └─rlang::abort(...) Created on 2023-08-07 with reprex v2.0.2 Maybe @jmobrien from the |
@rempsyc happy to discuss more over on the |
Maybe this is expected/not a bug, but it seems that
case_when()
withinmutate()
loses variable labels, althoughmutate()
alone or withreplace()
doesn't. Reprex:Created on 2023-05-23 with reprex v2.0.2
Could it be because
case_when()
is vectorized? And given that I wish to preserve labels, what workaround would you recommend? Falling back toreplace()
? Thank you.Edit:
Linked to #5762. Which suggested
recode()
as the workaround. However,recode()
has been superseded bycase_match()
.But
case_match()
does not seem to solve the issue:Created on 2023-05-23 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: