0
DV<-DV %>%
      mutate(outcome_diff=case_when(
  is.numeric(outcome) ~ outcome1-outcome2
  is.factor(outcome) ~ ifelse(outcome1==outcome2,"agree","disagree"), 
  IV$outcome==NA|IV$outcome2==NA ~NA_character_,  
  TRUE ~ NA_character_)) 

In the previous code, we successed to make a loop to generatate many outcomes. for each outcome, we generate outcome1 and outcome2.

In the presented code, What I tried to do was

  1. if the outcome variable is numeric, generate outcome_diff variable by (outcome1-outcome2)
  2. if the outcome variables is factors, do this: (ifelse(outcome1==outcome2, "agree","disagree").

However, I faced a problem that NA_character_ generates errors. Moreover, I guess case_When() function does not like to deal with different class vectors.

is there any better way to do this?

Thanks.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
yoo
  • 491
  • 3
  • 10
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. is `IV` a completely different data.frame than `DV`? – MrFlick Jul 21 '21 at 18:43
  • 1
    To test for NA, use is.na(). Nothing else will work as expected, including is.numeric() or x == NA. – Bill O'Brien Jul 21 '21 at 18:45

0 Answers0