1

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.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
ESKim
  • 422
  • 4
  • 14
  • In your sample data there are no columns `outcome1` and `outcome2`. Can you show the expected output? Don't forget to [set a seed](https://stackoverflow.com/questions/13605271/reasons-for-using-the-set-seed-function) for reproducibility. – markus Jun 20 '19 at 07:46
  • Oh, I made serious mistake. I corrected it. Thanks for your noticing – ESKim Jun 20 '19 at 08:04

1 Answers1

0

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

df[df$missing==1,c("cond1", "cond2")] <- NA
Esben Eickhardt
  • 3,183
  • 2
  • 35
  • 56