Search code examples
rdata-cleaningdata-wranglinglikert

Recoding Multiple Likert-scale Columns at Once


I usually do this the hard way, but I'm sure one of you coding experts has something less tedious.

Using this data set below:

#Example Dataset
  Q1 <- c("Agree", "Disagree", "Strongly Agree", "Strongly Disagree", "Strongly Disagree")
  Q2 <- c("Disagree", "Disagree", "Agree", "Disagree", "Strongly Disagree")
  Q3 <- c("Agree", "Disagree", "Strongly Agree", "Strongly Agree", "Strongly Agree")
  Q4 <- c("Disagree", "Disagree", "Disagree", "Strongly Disagree", "Strongly Disagree")

  data <- data.frame(Q1, Q2, Q3, Q4)

I would usually just recode it using the car package like this:

library(car)

data$Q1 <- car::recode(data$Q1, "
                       'Strongly Disagree' = 1; 
                       'Disagree' = 2; 
                       'Agree' = 3; 
                       'Strongly Agree' = 4")
data$Q2 <- car::recode(data$Q2, "
                       'Strongly Disagree' = 1; 
                       'Disagree' = 2; 
                       'Agree' = 3; 
                       'Strongly Agree' = 4")
data$Q3 <- car::recode(data$Q3, "
                       'Strongly Disagree' = 1; 
                       'Disagree' = 2; 
                       'Agree' = 3; 
                       'Strongly Agree' = 4")
data$Q4 <- car::recode(data$Q4, "
                       'Strongly Disagree' = 1; 
                       'Disagree' = 2; 
                       'Agree' = 3; 
                       'Strongly Agree' = 4")

Is there a way I can do this by column? Or, applying to the whole scale with less code?

I thought I could just specify a range of columns maybe like this, but it does not do what I want.

data <- recode(data$[1:4], "
                  'Strongly Disagree' = 1; 
                  'Disagree' = 2; 
                  'Agree' = 3; 
                  'Strongly Agree' = 4")

Solution

  • Alright, this is the solution. It's that dplyr is superior.

    datarecoded <- data %>%                                  #select dataframe
      mutate_at(c("Q1", "Q2", "Q3", "Q4"),                   #select columns
                funs(dplyr::recode(.,"Strongly Disagree" =1, #enter recodes
                                   "Disagree" =2, 
                                   "Agree" =3, 
                                   "Strongly Agree" =4)))
    

    Result

    Q1 Q2 Q3 Q4
    1  3  2  3  2
    2  2  2  2  2
    3  4  3  4  2
    4  1  2  4  1
    5  1  1  4  1
    

    You can also select a range within the dataframe.

    datarecoded <- data %>% 
      mutate_at(c(1:3), 
                funs(dplyr::recode(.,"Strongly Disagree" =1, 
                                   "Disagree" =2, 
                                   "Agree" =3, 
                                   "Strongly Agree" =4)))
    

    Result 2

      Q1 Q2 Q3                Q4
    1  3  2  3          Disagree
    2  2  2  2          Disagree
    3  4  3  4          Disagree
    4  1  2  4 Strongly Disagree
    5  1  1  4 Strongly Disagree