can someone please help me with creating a new variable in R? I need to tell R something like this:
data <- data %>%
mutate(c = if_else(x == y, x, ifelse(x != y, y, ifelse(is.na(x), y, ifelse(is.na(y), x, NA))))
of course, in this form, it doesn't work. If a value in the first column is equal to a value from the second column - use the value from the first column, if they are not equal - then use the value from the second column, if there is NA in the first column, but there is some value in the second column, then use the value from the second column. if there is NA in the second column, but there is some value in the first column, then use the value from the first column. (+ the values in x and y are characters)
this is the outcome in the new variable "c" I wish for:
x | y | c |
---|---|---|
1 | 1 | 1 |
1 | 3 | 3 |
NA | 5 | 5 |
6 | NA | 6 |
NA | NA | NA |