I'm trying to use dplyr's across and case_when across my entire dataset, so whenever it sees "Strongly Agree" it changes it to a numeric 5, "Agree" to a numeric 4, and so on. I've tried looking at this answer, but I'm getting an error because my dataset has logical and numeric columns and R rightfully says that "Agree" can't be in a logical column, etc.
Here's my data:
library(dplyr)
test <- tibble(name = c("Justin", "Corey", "Sibley"),
date = c("2021-08-09", "2021-10-29", "2021-01-01"),
s1 = c("Agree", "Neutral", "Strongly Disagree"),
s2rl = c("Agree", "Neutral", "Strongly Disagree"),
f1 = c("Strongly Agree", "Disagree", "Strongly Disagree"),
f2rl = c("Strongly Agree", "Disagree", "Strongly Disagree"),
exam = c(90, 99, 100),
early = c(TRUE, FALSE, FALSE))
Ideally, I'd like one command that would allow me to go across the entire dataset. However, if that can't be done, I'd like to have one argument that would allow me to use multiple across(contains()) arguments (i.e., here contains "s" or "f").
Here's what I've tried already to no avail:
library(dplyr)
test %>%
mutate(across(.),
~ case_when(. == "Strongly Agree" ~ 5,
. == "Agree" ~ 4,
. == "Neutral" ~ 3,
. == "Disagree" ~ 2,
. == "Strongly Disagree" ~ 1,
TRUE ~ NA))
Error: Problem with `mutate()` input `..1`.
x Must subset columns with a valid subscript vector.
x Subscript has the wrong type `tbl_df<
name: character
date: character
s1 : character
s2rl: character
f1 : character
f2rl: character
exam: double
>`.
ℹ It must be numeric or character.
ℹ Input `..1` is `across(.)`.