I have a dataset like the following simplified one:
x_1 <- c(1, NA, 2, 3, NA, 4, 5)
x_2 <- c(2, 1, NA, NA, NA, 4, 6)
y_1 <- c(2, 4, 6, 8, NA, 10, NA)
y_2 <- c(NA, 4, NA, 8, 10, 11, 13)
df <- data.frame(x_1, x_2, y_1, y_2)
x_1 x_2 y_1 y_2
1 1 2 2 NA
2 NA 1 4 4
3 2 NA 6 NA
4 3 NA 8 8
5 NA NA NA 10
6 4 4 10 11
7 5 6 NA 13
The goal is to coalesce each of the two corresponding variables (x and y) and to replace the values that are not the same (e.g. first row of x_1 and x_2) with NA. I did this with the following:
df <- df %>%
mutate(x = coalesce(x_1, x_2)) %>%
mutate(x = ifelse(!is.na(x) &
!is.na(x_2) &
x != x_2,
NA,
x)) %>%
select(!c(x_1, x_2))
Now, I have to do this with 21 variables so I thought I put the variables in a list and feed them through the pipeline with a for loop like this:
cols <- c("x", "y")
for(i in cols){
var_1 <- paste(i, "1", sep = "_")
var_2 <- paste(i, "2", sep = "_")
df <- df %>%
mutate(i = coalesce(var_1, var_2)) %>%
mutate(i = ifelse(!is.na(i) &
!is.na(var_2) &
i != var_2,
NA,
i)) %>%
select(!c(var_1, var_2))
}
What happens is that the code is executed, but instead of the new variables there is only the variable "i" with empty values. It seems as if R does not recognise the "i" in the pipeline as the iterator, however it does recognize "var_1" and "var_2" (because they are being removed from the dataset).
Does anyone know why that is and how I can fix it?
Thanks a lot in advance.