Let's say I have the following df:
mydf <- data.frame(col1 = c("Red", "Red", "Blue", "Orange"),
col2 = c("Red", "Blue", NA, "Red"),
col3 = c("Red", "Red", "Blue", "Red"),
col4 = c("Red", "Red", "Blue", "Blue"))
I'd like to create a column called "all_equal"
that is set to 1 if all non_NA values across columns 1-4 are equal to the same value. This should result in the following:
col1 col2 col3 col4 all_true
1 Red Red Red Red TRUE
2 Red Blue Red Red FALSE
3 Blue <NA> Blue Blue TRUE
4 Orange Red Red Blue FALSE
Note that the NA in column two should not count against equality. I've tried using all
to test for equality, but it seems to not work well in dplyr chains.