'How to write in R to indicate if the treatment indicator 1/0 and non-zero?
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?
Solution 1:[1]
a <- ifelse(sum(x) == 0, 0, 1)
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stomper |
| Solution 2 | SeaJane |
