'Create variable (dummy) depending on values of two other columns in R
I would like to create a dummy that assumes 1 if A==1 and 0 if B==1. Note that A and B are dummies but with NA instead of 0. I also have observations for which neither A or B assumes value 1 and want to exclude them (rows with NAs in both columns)
structure(list(id = 1:11, A = c(1L, NA, 1L, NA, 1L, 1L, NA, 1L,
NA, NA, NA), B = c(NA, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, 1L
)), class = "data.frame", row.names = c(NA, -11L))
id A B
1 1 1 NA
2 2 NA 1
3 3 1 NA
4 4 NA 1
5 5 1 NA
6 6 1 NA
7 7 NA NA
8 8 1 NA
9 9 NA NA
10 10 NA NA
11 11 NA 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
