Hi,
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
library(tidyverse)
iris_with_two_alternatives <- iris %>%
tibble() %>%
mutate_at(vars(Sepal.Length:Petal.Width), ~.*5) %>%
rename_all(
funs(paste0(., ".alternative"))) %>%
bind_cols(iris)
Pick one of two alternatives
iris_alternative_cols_chosen <- iris_with_two_alternatives %>%
mutate_at(vars(Sepal.Length:Petal.Width),
~case_when(
. > 1 & . < 4 ~ vars(paste0(., ".alternative")),
. <= 1 | . >=5, ~ . ,
. >=4 & . <5, ~ .+1 )) %>%
select(Sepal.Length:Petal.Width)
The above FAILS, producing:
Error:Problem with mutate()
input Sepal.Length
.
x Case 2 (. > 1 & . < 4 ~ vars(paste0(., ".alternative"))
) must be a two-sided formula, not a logical vector.
Input Sepal.Length
is (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()).
Thanks!
Dylan