At the moment, I'm recoding my Likert type scales into numeric values. However, I have many items from a number of different scales in this dataset. For example: Q2.1_1: Q2.1_16 is one scale with a Likert scale that has a different key than the other surveys. At the moment, I'm manually entering each recode like this:
final$Q2.1_1rc <- as.numeric(recode(
final$Q2.1_1,
"Very slightly or not at all" = 1,
"A little" = 2,
"Moderately" = 3,
"Quite a bit" = 4,
"Extremely" = 5
))
I then C/P and continue to change the name of variable.. However, I have a large dataset so doing this manually will be cumbersome. Can anyone help me out with a shorter way to code this? Are there packages that help with this? Perhaps a function?
Thanks!
One way you could recode the multiple variables at once is to use the mutate_at
function from the dplyr
package.
Example data
library(dplyr)
set.seed(123)
resp <- c("Very slightly or not at all", "A little", "Moderately", "Quite a bit", "Extremely")
final <- tibble(Q2.1_1 = sample(resp, 6, replace = TRUE),
Q2.2_1 = sample(resp, 6, replace = TRUE))
Solution
Assuming the variables you want to start with in final
all have "Q2"
at the start of their variable name, you could do this:
final %>%
mutate_at(vars(starts_with("Q2")),
funs("rc" = recode(.,
"Very slightly or not at all" = 1,
"A little" = 2,
"Moderately" = 3,
"Quite a bit" = 4,
"Extremely" = 5)))
#> # A tibble: 6 x 4
#> Q2.1_1 Q2.2_1 Q2.1_1_rc Q2.2_1_rc
#> <chr> <chr> <dbl> <dbl>
#> 1 A little Moderately 2 3
#> 2 Quite a bit Extremely 4 5
#> 3 Moderately Moderately 3 3
#> 4 Extremely Moderately 5 3
#> 5 Extremely Extremely 5 5
#> 6 Very slightly or not at all Moderately 1 3
As stated in the official documentation, the best place to get started with dplyr
is the data import chapter of the R for Data Science book while specific examples related to the use of mutate_at
can be found here.