Reputation: 21
I have data from a survey with Likert scores taken at three time points. Currently in numeric form, I'd like to convert all the columns with likert score data from numeric to factors with specified levels. Rather than completing the function below 21 times for each column, I'd like to perform the function across 21 columns in one chunk.
data$Pre_S1 <- factor(data$Pre_S1,
levels = c(1, 2, 3, 4, 5, 6),
labels = c(
"Strongly Disagree", "Moderately Disagree",
"Slightly Disagree", "Slightly Agree",
"Moderately Agree", "Strongly Agree"
)
)
I feel I might be close with this code but don't quite have it yet. Any input is much appreciated.
data %>% mutate(across(
c(
"Pre_S1", "Pre_S2", "Pre_S3", "Pre_S4", "Pre_S5",
"Pre_S6", "Pre_S7",
"Mid_S1", "Mid_S2", "Mid_S3",
"Mid_S4", "Mid_S5", "Mid_S6", "Mid_S7",
"Post_S1", "Post_S2", "Post_S3",
"Post_S4", "Post_S5", "Post_S6", "Post_S7"
),
as.factor(data,
levels = c(1, 2, 3, 4, 5, 6),
labels = c(
"Strongly Disagree", "Moderately Disagree",
"Slightly Disagree", "Slightly Agree",
"Moderately Agree", "Strongly Agree"
)
)
))
I can post some data if that helps. TIA.
I have tried changing each value with a single line of code, though this is repetitive & R doesn't seem to like it much.
Upvotes: 1
Views: 88
Reputation: 33663
A base R alternative:
convert_factor <- function(x) {
factor(
x, levels = c(1, 2, 3, 4, 5, 6),
labels = c(
"Strongly Disagree", "Moderately Disagree",
"Slightly Disagree", "Slightly Agree",
"Moderately Agree", "Strongly Agree"
)
)
}
vars <- c("Pre_S1", "Pre_S2", "Mid_S1", "Mid_S2", ...)
data[vars] <- lapply(data[vars], convert_factor)
Upvotes: 1
Reputation: 125727
Using some fake example data here is one approach to make your code work which converts all columns ending on _S
+ a number:
library(dplyr, warn=FALSE)
set.seed(123)
data <- tibble(
ID = 1:10,
Pre_S1 = sample(1:6, 10, replace = TRUE),
Pre_S2 = sample(1:6, 10, replace = TRUE),
Mid_S1 = sample(1:6, 10, replace = TRUE),
Mid_S2 = sample(1:6, 10, replace = TRUE)
)
data <- data |>
mutate(
across(
matches("_S\\d+$"),
~ factor(.x,
levels = c(1, 2, 3, 4, 5, 6),
labels = c(
"Strongly Disagree", "Moderately Disagree",
"Slightly Disagree", "Slightly Agree",
"Moderately Agree", "Strongly Agree"
)
)
)
)
str(data)
#> tibble [10 × 5] (S3: tbl_df/tbl/data.frame)
#> $ ID : int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ Pre_S1: Factor w/ 6 levels "Strongly Disagree",..: 3 6 3 2 2 6 3 5 4 6
#> $ Pre_S2: Factor w/ 6 levels "Strongly Disagree",..: 6 1 2 3 5 3 3 1 4 1
#> $ Mid_S1: Factor w/ 6 levels "Strongly Disagree",..: 1 5 3 2 2 1 6 3 4 6
#> $ Mid_S2: Factor w/ 6 levels "Strongly Disagree",..: 1 3 5 4 2 5 1 1 2 3
Upvotes: 1