I'll start with some example data:
set.seed(1)
exampledata <- data.frame(
a = sample(1:5, 10, replace = TRUE),
b = sample(1:5, 10, replace = TRUE),
c = sample(1:5, 10, replace = TRUE),
a_long = sample(1:5, 10, replace = TRUE),
b_long = sample(1:5, 10, replace = TRUE),
c_long = sample(1:5, 10, replace = TRUE)
)
I am trying to replace the values in columns a
to c
with recoded values from the corresponding _long
columns, when a condition on the original column is met. This accomplishes what I want but doesn't scale well:
library(tidyverse)
library(sjmisc)
exampledata %>%
mutate(
a = case_when(
a %% 2 == 1 ~ rec(a_long, rec = "1=4000; 2=12000; 3=20000; 4=30000; 5=42000"),
TRUE ~ as.numeric(a)
),
b = case_when(
b %% 2 == 1 ~ rec(b_long, rec = "1=4000; 2=12000; 3=20000; 4=30000; 5=42000"),
TRUE ~ as.numeric(b)
),
c = case_when(
c %% 2 == 1 ~ rec(c_long, rec = "1=4000; 2=12000; 3=20000; 4=30000; 5=42000"),
TRUE ~ as.numeric(c)
)
)
Here is my attempt at doing this for all columns in one go:
exampledata %>%
mutate(across(c(a, b, c), ~ case_when(
.x %% 2 == 1 ~ rec(paste0(cur_column(), "_long"), rec = "1=4000; 2=12000; 3=20000; 4=30000; 5=42000"),
TRUE ~ as.numeric(.x)
)))
However instead of using the recoded values it puts NA
's in the cells that satisfy the condition. I tried adding !!
before paste0(cur_column(), "_long")
but this throws an error:
Error: cur_column()
must only be used inside across()
.
My question essentially is how to paste together the column names so that I can feed them into rec()
.
I would refer to keep using rec()
and case_when()
because my real code uses multiple conditions and has more values to recode. Any help would be much appreciated.
question from:
https://stackoverflow.com/questions/65864759/recode-multiple-variables-using-across-case-when-and-rec-sjmisc