Search code examples
rmissing-datananiar

Converting values to NA with conditions in R


I'm trying to convert variables to NA(missing) with conditions

This is my data as example. I have 5 patients ID, If there is '1' in missing variable, outcome1 and outcome2 will be converted to NA(missing).

  ID<-c("a","b","c","d","e")
  cond1<-as.factor(sample(x=1:7,size=5,replace=TRUE))
  cond2<-as.factor(sample(x=1:7,size=5,replace=TRUE))
  cond3<-as.factor(sample(x=1:7,size=5,replace=TRUE))
  missing<-as.factor(sample(x=0:1,size=5,replace=TRUE))
  outcome1<-sample(x=1:10, size=5,replace=TRUE)
  outcome2<-sample(x=1:10, size=5,replace=TRUE)
  df<-data.frame(ID,cond1,cond2,cond3,missing,outcome1,outcome2)
  df
   ID cond1 cond2 cond3 missing outcome1 outcome2
1  a     7     1     7       0       6       5
2  b     5     3     7       0       3       1
3  c     4     5     1       1       3       9
4  d     2     2     3       0       7       3
5  e     1     7     4       1       2       7

I've found the replace_with_na_at function, in naniar package. however, it didn't worked.

df%>%
  replace_with_na_at(.vars=c("outcome1","outcome2"), condition = ~ hn10_14med$missing==1)

Error: Predicate functions must return a single `TRUE` or `FALSE`, not a logical vector of length 0
Call `rlang::last_error()` to see a backtrace

How to convert variable to NA with conditions? If there is a better way, though not using replace_with_na_at function, you would let me know.


Solution

  • If you want to replace the values in place, just use ifelse:

    df[df$missing==1,c("cond1", "cond2")] <- NA