-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
mis_val(,with_labels = TRUE) incompatible with mutate() #68
Comments
Hi, |
Thank you for your quick response! Here is the result of sessionInfo() just after running the error causing command:
Matrix products: default locale: attached base packages: other attached packages: loaded via a namespace (and not attached): |
UPDATE: Here is a minimum working example to illustrate the error: var1 <- haven::labelled(c(0, 1, 9, 0, 0),c( mytest <- tibble(var1,var2) mytest$var1 <- expss::mis_val(mytest$var1, c(9)) This mutate command works finemytest <- mytest %>% This mutate command causes the errormytest <- mytest %>% Here is the error I get: Error: Problem with Here is the sessionInfo() results:
|
Since this is a combination of packages causing the error, I also submitted the following request to dplyr: |
Thanks, now I can reproduce the issue. Currently I don't understand why it is happen. It is not about library(dplyr)
library(haven)
library(expss)
var2 <- haven::labelled(c(0, 1, 2, 3, 1), c(DK = 0, first = 1, second = 2, third = 3))
mytest <- tibble(var2)
mytest$var2 <- expss::mis_val(mytest$var2, c(0), with_labels = TRUE)
class(mytest$var2)
# remove vctrs_vctr class
class(mytest$var2) = setdiff(class(mytest$var2), "vctrs_vctr")
# works as expected
mytest <- mytest %>%
mutate(newvar2 = case_when(var2 == 1 ~ 1,
var2 != 1 ~ 0)) I will try to make a workaround for this issue in the next version. library(dplyr)
library(haven)
library(expss)
mygss <- expss::read_spss("~/GSS2018.sav") %>%
select(YEAR,ID,WRKSTAT,SEX,RACE,RINCOME,INCOME,WEALTH,REGION,
AGE,MARITAL,CHILDS,EDUC,DEGREE,
PARTYID,PRES16,NATEDUC,COURTS,TAX,NATFARE,NATENRGY,PARTTEAM,LIFE)
mygss$NATENRGY <- mis_val(mygss$NATENRGY, c(0,8,9),with_labels = TRUE)
mygss = mutate(mygss, renew = case_when(NATENRGY == 1 ~ 1,
NATENRGY != 1 ~ 0))
calc_cro(mygss,NATENRGY,renew) or, if you want to go with haven: library(dplyr)
library(haven)
library(expss)
mygss <- haven::read_spss("~/GSS2018.sav") %>%
select(YEAR,ID,WRKSTAT,SEX,RACE,RINCOME,INCOME,WEALTH,REGION,
AGE,MARITAL,CHILDS,EDUC,DEGREE,
PARTYID,PRES16,NATEDUC,COURTS,TAX,NATFARE,NATENRGY,PARTTEAM,LIFE)
mygss = add_labelled_class(mygss, remove_classes = c("haven_labelled", "vctrs_vctr" ))
mygss$NATENRGY <- mis_val(mygss$NATENRGY, c(0,8,9),with_labels = TRUE)
mygss = mutate(mygss, renew = case_when(NATENRGY == 1 ~ 1,
NATENRGY != 1 ~ 0))
calc_cro(mygss,NATENRGY,renew) |
Thank you for your quick response and helpful feedback! I agree that this seems like an error caused by different approaches to labels by haven in the tidyverse and in expss. As I mentioned in a previous comment, I did submit this issue to dplyr. I try to work within the tidyverse as much as possible, which is why I use haven to read in the SPSS file. But, there is no tidyverse package for creating the wide variety of human-readable tables that expss provides. Thank you again for providing the expss package. |
The "with_labels = TRUE" option in the "mis_val()" command changes the class of the variable to "labelled". This variable no longer be used as input into mutate() commands.
See example below:
I read in the GSS.sav file
mygss <- as.data.frame(read_sav("GSS2018.sav")) %>%
select(YEAR,ID,WRKSTAT,SEX,RACE,RINCOME,INCOME,WEALTH,REGION,
AGE,MARITAL,CHILDS,EDUC,DEGREE,
PARTYID,PRES16,NATEDUC,COURTS,TAX,NATFARE,NATENRGY,PARTTEAM,LIFE)
Here is the structure of the variable before recording with mis_val()
str(mygss$NATENRGY)
dbl+lbl [1:2348] 1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, NA, 1, N...
@ label : chr "Developing alternative energy sources"
@ format.spss : chr "F1.0"
@ display_width: int 10
@ labels : Named num [1:6] 0 1 2 3 8 9
..- attr(*, "names")= chr [1:6] "IAP" "Too little" "About right" "Too much" …
mis_val does not change type if with_labels option is not used
The recoded variable works fine with mutate:
| | | renew | |
But, the labels are left in there, so I use the with_labels = TRUE option
mis_val using the with_labels = TRUE option changes the class of the variable
Now the variable is of the 'labelled' class
labelled class variables cannot be used in the mutate() commands
Error: Problem with
mutate()
inputrenew
.x Can't combine
..1
<labelled> and..2
.ℹ Input
renew
iscase_when(NATENRGY == 1 ~ 1, NATENRGY != 1 ~ 0)
.The text was updated successfully, but these errors were encountered: