How to recode values from text to numbers - likert scale

I have this dataset I have loaded in called "Data". Within this dataset, it includes various likert scale questionnaires. Using an example, in this dataset I have variables EQ1 - EQ10 which represents a 10 item empathy questionnaire. Currently in this likert scale, it is:

  • Strongly agree
  • Slightly agree
  • Strongly disagree
  • Slightly disagree

For items EQ1, 2, 4, 6 and 9, Strongly agree = 2, Slightly agree = 1 and the other 2 options = 0. Then the other items need to be reverse coded. I am really struggling with how to do this.

I've tried this:
Data <- read.csv ("data.csv")

data <- data %>%
mutate(
EQ1 = recode(EQ1, "strongly agree" = 2, "slightly agree" = 1, "strongly disagree" = 0, "slightly disagree" = 0, .default = NA),
EQ2 = recode(EQ2, "strongly agree" = 2, "slightly agree" = 1, "strongly disagree" = 0, "slightly disagree" = 0, .default = NA),
EQ4 = recode(EQ4, "strongly agree" = 2, "slightly agree" = 1, "strongly disagree" = 0, "slightly disagree" = 0, .default = NA),
EQ6 = recode(EQ6, "strongly agree" = 2, "slightly agree" = 1, "strongly disagree" = 0, "slightly disagree" = 0, .default = NA),
EQ9 = recode(EQ9, "strongly agree" = 2, "slightly agree" = 1, "strongly disagree" = 0, "slightly disagree" = 0, .default = NA))

But I get this error: Error in UseMethod("mutate") :
no applicable method for 'mutate' applied to an object of class "function"

Please help

First, R is case sensitive. Your data.csv file is imported as 'Data', but you use 'data' in the code pipe.

Second, there is a data() function in base R, so you are starting the pipe with a function name, not a data set.

Try Data <- Data %>% at the start of the pipe.

Error in mutate():
! Problem while computing EQ1 = recode(...).
Caused by error in recode():
! .default must be a double vector, not a logical vector.
Run rlang::last_error() to see where the error occurred.

I now get this error?

Change .default=NA to .default = as.numeric(NA)

By the way, the help for recode says it has been superseded by case_match.

You're actually a legend. It seems to be working now. Thank you so much, I've been stressing over it the whole day trying to figure it out myself.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.