I have a dataset with two alternative measures for each variable. I want to create a new data table that chooses between each alternative, depending on the specified conditions. Here is a reproducible example using the iris dataset.
generate the dataset, with two similarly named alternatives
iris_with_two_alternatives <- iris %>%
mutate_at(vars(Sepal.Length:Petal.Width), ~.*5) %>%
funs(paste0(., ".alternative"))) %>%
Pick one of two alternatives
iris_alternative_cols_chosen <- iris_with_two_alternatives %>%
. > 1 & . < 4 ~ vars(paste0(., ".alternative")),
. <= 1 | . >=5, ~ . ,
. >=4 & . <5, ~ .+1 )) %>%
The above FAILS, producing:
x Case 2 (
. > 1 & . < 4 ~ vars(paste0(., ".alternative"))) must be a two-sided formula, not a logical vector.
(structure(function (..., .x = ..1, .y = ..2, . = ..1) ....
I know at least one issue is in line 4, where I'm providing cases when I want to use the similarly named variable. I don't know how to indicate (wrap? embrace?) that the result of the paste is not just text, but the corresponding variable from the indicated column.
Second, I think I'm supposed to be using mutate(across()), but am having trouble using that in place of mutate_at(), so an additional thanks if you can get it working with mutate(across()).