Given I have a data frame like this:
x <- data.frame(d1 = c(1,0,1,3,5), d2 = c(0,0,1,1,0))
# d1 d2
# 1 1 0
# 2 0 0
# 3 1 1
# 4 3 1
# 5 5 0
How can I create a new column equal to 1 if either c1 or c2 is equal to 1, and 0 if neither is? Like this:
# d1 d2 R
# 1 1 0 1
# 2 0 0 0
# 3 1 1 1
# 4 3 1 1
# 5 5 0 0
Using dplyr, I'm able to accomplish this by first splitting the data frame using filter_at then binding the resulting data frames together again.
x1 <- x %>%
filter_at(vars(starts_with("d")), any_vars(. == 1))
x1$R <- 1
x2 <- x %>%
filter_at(vars(starts_with("d")), all_vars(. != 1))
x2$R <- 0
x <- bind_rows(x1,x2)
# d1 d2 R
# 1 1 0 1
# 2 1 1 1
# 3 3 1 1
# 4 0 0 0
# 5 5 0 0
However not only does this method seem like a roundabout way to complete this task, the data frame order gets changed. Not an issue in my case, but might be annoying in other situations. Does anyone know of a better way to do this?
mutate(x, R = as.numeric(d1 == 1 | d2 == 1))
? – Axemanifelse()
could be useful, which could be used withinmutate()
. – aosmith