Home > Back-end >  How to order the level of multiple variables?
How to order the level of multiple variables?

Time:11-23

I have a dataset like that:

Sex q1 q... q10
Male Agree Strongly agree Strongly disagree
Female Desagree Agree Disagree
Male Agree Strongly agree Disagree
Female Desagree Agree Strongly disagree

I've been doing this:

df$q1 <- factor(df$q1, levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"))
df$q2 <- factor(df$q2, levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"))
df$qn <- factor(df$qn, levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"))

So I'm seeking for a smart way to order these levels to all the q1:qn subset of my dataset.

CodePudding user response:

Using dplyr::across():

library(dplyr)

df <- df %>%
  mutate(across(
    q1:q4,
    factor,
    levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree")
  ))

levels(df$q3)
# "Strongly disagree" "Disagree"          "Agree" "Strongly agree"

Example data:

set.seed(13)

responses <- c("Strongly disagree", "Disagree", "Agree", "Strongly agree")

df <- data.frame(
  Sex = sample(c("Male", "Female"), 5, replace = TRUE),
  q1 = sample(responses, 5, replace = TRUE),
  q2 = sample(responses, 5, replace = TRUE),
  q3 = sample(responses, 5, replace = TRUE),
  q4 = sample(responses, 5, replace = TRUE)
)

CodePudding user response:

A base R approach using lapply may look like so:

Note: There was (probably) a typo in your data, i.e. Desagree which results in NAs when converting to a factor.

levels <- c("Strongly disagree", "Disagree", "Agree", "Strongly agree")
cols <- names(df)[grepl("^q\\d", names(df))]
df[cols] <- lapply(df[cols], factor, levels = levels)

str(df)
#> 'data.frame':    4 obs. of  4 variables:
#>  $ ex : chr  "Male" "Female" "Male" "Female"
#>  $ q1 : Factor w/ 4 levels "Strongly disagree",..: 3 NA 3 NA
#>  $ q2 : Factor w/ 4 levels "Strongly disagree",..: 4 3 4 3
#>  $ q10: Factor w/ 4 levels "Strongly disagree",..: 1 2 2 1

DATA

df <- data.frame(
  ex = c("Male", "Female", "Male", "Female"),
  q1 = c("Agree", "Desagree", "Agree", "Desagree"),
  q2 = c("Strongly agree", "Agree", "Strongly agree", "Agree"),
  q10 = c(
    "Strongly disagree", "Disagree",
    "Disagree", "Strongly disagree"
  )
)
  • Related