There are many items (25 in the SDQ: strengths and difficulties scale) for which I would like to coalesce the 2to4yrs and 5to17yrs versions into the 2to4yrs columns.
library(tidyverse)
df <- data.frame(
sdq2to4yrs_item1 = c(1, NA, NA),
sdq5to17yrs_item1 = c(NA, NA, 2),
sdq2to4yrs_item2 = c(2, 2, NA),
sdq5to17yrs_item2 = c(1, 2, 3)
)
df
#> sdq2to4yrs_item1 sdq5to17yrs_item1 sdq2to4yrs_item2 sdq5to17yrs_item2
#> 1 1 NA 2 1
#> 2 NA NA 2 2
#> 3 NA 2 NA 3
## What I'm after
data.frame(
sdq2to4yrs_item1 = c(1, NA, 2),
sdq2to4yrs_item2 = c(2, 2, 3)
)
#> sdq2to4yrs_item1 sdq2to4yrs_item2
#> 1 1 2
#> 2 NA 2
#> 3 2 3
## The code I'd like to work
df %>%
mutate(
across(
matches("2to4yrs"),
~ coalesce(
!!!select(., matches(
cur_column() %>% str_remove(".*yrs_")
)
)
)
)
)
#> Error in local_error_context(dots = dots, .index = i, mask = mask): promise already under evaluation: recursive default argument reference or earlier problems?
Created on 2022-08-24 by the reprex package (v2.0.1)