Home > Software engineering >  How to write in R to indicate if the treatment indicator 1/0 and non-zero?
How to write in R to indicate if the treatment indicator 1/0 and non-zero?

Time:03-05

the treatment indicator will a variable that equals 1 if the number of videos included in an article (num_videos) is non-zero, and which equals 0 otherwise. How do you write this in R? Is it the function ifelse in R?

CodePudding user response:

 a <- ifelse(sum(x) == 0, 0, 1) 

CodePudding user response:

You can also use case_when() if you have more than two options like having a catch for NA values (I flagged them with a 2).

treatment <- c(0, 1, 2, 3, 4, NA, 2)
case_when(treatment == 0 ~ 0, treatment != 0  ~ 1, is.na(treatment) ~ 2)
  •  Tags:  
  • r
  • Related