Just recently began my journey in R and finally getting the hang of things. I have a very simple question yet I'm unable to find the answer I'm looking for.
I have a dataset of survey responses that were collected using a Likert type scale. Some of the range from very strongly disagree to the opposite (1-7) while others are 1-5. I'm looking for a simple way of recoding each column within the dataset, preferably using the dplyr package as I'm trying to master it. So far, I have this:
df is my dataframe which contains columns Q2.1_1: Q2.5_1. I need the new recoded columns to be numeric as they are currently factors (I want to later run descriptives).
The problem with this line is that it creates a vector that recodes it, but not within my df dataframe. I'm not sure if I should be appending this to df or if there's a better way to just edit within df itself.
as.numeric(recode(df$Q2.1_1, "Very slightly or not at all"=1, .... etc))
I think you're looking for something like the following:
mydata <- data.frame(x = c("A lot", "Some", "Not at all")) mydata <- mutate(mydata, x_recoded = recode(x, "A lot" = 1, "Some" = 2, "Not at all" = 3)) mydata x x_recoded 1 A lot 1 2 Some 2 3 Not at all 3
This code is:
mutate()function to create a new variable
mydataback to itself so it now has
recode infers that the variable is numeric since numbers are on the right-hand-side in the recodes, so you don't need to use