set.seed(3)
library(dplyr)
x <- tibble(Measure = c("Height","Weight","Width","Length"),
AD1_1= rpois(4,10),
AD1_2= rpois(4,9),
AD2_1= rpois(4,10),
AD2_2= rpois(4,9),
AD3_1= rpois(4,10),
AD3_2= rpois(4,9))
Suppose I have data that looks like this. I wish to run a function for each AD, paired with underscored number, i.e., AD1fun, AD2fun,AD3fun.
Instead of writing,
fun <- function(x,y){x-y}
dat %>%
mutate(AD1fun = fun(AD1_1,AD1_2),
AD2fun = fun(AD2_1,AD2_2),
...)
Finding the differences of paired-columns using dplyr shows that
x_minus <- x %>%
mutate(fun(across(ends_with("_1"), .names = "{col}_minus"), across(ends_with("_2")))) %>%
rename_with(~ sub("_\\d+", "", .), ends_with("_minus"))
can be used to produce
# A tibble: 4 x 10
Measure AD1_1 AD1_2 AD2_1 AD2_2 AD3_1 AD3_2 AD1_minus AD2_minus AD3_minus
<chr> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 Height 6 10 10 3 12 8 -4 7 4
2 Weight 8 9 13 6 14 7 -1 7 7
3 Width 10 9 11 5 12 8 1 6 4
4 Length 8 9 8 7 8 13 -1 1 -5
However, if we were to make non-operational function,
fun <- function(x,y){
case <- case_when(
x == y ~ "Agree",
x == 0 & y != 0 ~ "Disagreement",
x != 0 & y == 0 ~ "Disagreement",
x-y <= 1 & x-y >= -1 ~ "Agree",
TRUE ~ "Disagree"
)
return(case)
}
x_case <- x %>%
mutate(fun(across(ends_with("_1"), .names = "{col}_case"), across(ends_with("_2")))) %>%
rename_with(~ sub("_\\d+", "", .), ends_with("_case"))
it will produce an error, since to quote,
This procedure essentially means that you compare two datasets: one with variables ending with _1 and one with _2. It is, thus, the same as dat %>% select(ends_with("_1")) - dat %>% select(ends_with("_2")). And as these are lists, you cannot compare them that way
If so, what can be done to include a function using across()?