Reputation: 623
I have a dataframe column consisting of numbers (e.g. with unique values of 1, 2, and 99).
library(dplyr)
df <- data.frame(A = c(1,2,1,1,99,2,1,99,1))
I want to change all rows where the value is 99 so that the value is NA instead. I tried doing this with mutate()
and case_when()
:
df <- mutate(df, A = case_when(A==99 ~ NA))
...but it converts all rows to NA (rather than just the rows where the value is 99).
Previous Stackoverflow questions similar to this (e.g. this one) state that using NA_double_
, NA_integer_
, NA_real_
etc fixes this. I tried those but this either:
(a) changes all values in the dummy example I've given here, or (b) I get an error on my real data (which is a haven_labelled variable):
Error in 'mutate()':
In argument: 'A = case_when(...)'.
Caused by error in 'case_when()':
! Failed to evaluate the right-hand side of formula 1.
Caused by error:
! object 'NA_double_' not found
I know I can set the correct values, and only the correct values, successfully using the method below:
df$A[df$A==99] <- NA
... but I'd like to understand how to do this using mutate()
and case_when()
.
What should I change my code to so that it works using mutate()
and case_when()
, whether it's (a) the dummy data presented in this question or (b) the haven_labelled data in my project?
Note: This is not a duplicate of this question because I am asking specifically about using case_when()
with mutate()
.
Upvotes: 0
Views: 25