record Likert scale values

Hello, I have a similar issue and I was hoping that somebody could help me resolve it. I'm trying to recode Likert scale values using this
code:

MyData2 <- MyData1 %>%
mutate_at(vars("MIPIP_6", "MIPIP_7", "MIPIP_8", "MIPIP_9", "MIPIP_10", "MIPIP_15", "MIPIP_16", "MIPIP_17", "MIPIP_18", "MIPIP_19", "MIPIP_20"), ~recode(., 1=5, 2=4, 3=3, 4=2, 5=1))

and I get this error message:
Error: unexpected '=' in:
" "MIPIP_15", "MIPIP_16", "MIPIP_17", "MIPIP_18",
"MIPIP_19", "MIPIP_20"), ~recode(., 1="

My data is characters, but I get the same error message if I make it numeric before running the code. Any suggestions would be greatly appreciated. Thank you!

Try changing the recode step to this:

~recode(., "1"=5, "2"=4, "3"=3, "4"=2, "5"=1))

The values on the left side of each recode pair have to be inside quotes or backticks if they're not legal R object names (and legal R names can't start with a number).

A couple of additional suggestions:

  1. It looks like you're reversing the numeric values. Instead of a recode, another option is to subtract each value from 6:

    ~ 6 - .
    
    # Small example
    list(1:5, 6 - 1:5)
    
    ## [[1]]
    ## [1] 1 2 3 4 5
    
    ## [[2]]
    ## [1] 5 4 3 2 1
    
  2. Instead of listing every column explicitly you could do:

    vars(paste0("MIPIP_", c(6:10,15:20))

Thank you so much for your response. I've tried your suggestions with the following results:

  1. Using:
    ~recode(., "1"=5, "2"=4, "3"=3, "4"=2, "5"=1))
    I now get the following error:
    Error: Problem with mutate() input MIPIP_6.
    x unused arguments (1 = 5, 2 = 4, 3 = 3, 4 = 2, 5 = 1)
    :information_source: Input MIPIP_6 is (structure(function (..., .x = ..1, .y = ..2, . = ..1) ....

Not sure what it means.

I've also tried ~ 6 - , but the outcome was not calculated properly. Not sure what I'm doing wrong - I'm trying to recode the Mini IPIP scale to calculate the mean for each personality scale - it's a standard procedure and i'm sure people have done it millions of times using R, but for some reason it doesn't work for me.

as @joels mentioned, it is helpful to include a reprex so others are able to help. I'd highly recommend the reprex package for this.

Here's how I would approach this:

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.5.3
#> Warning: package 'tibble' was built under R version 3.5.3
#> Warning: package 'tidyr' was built under R version 3.5.3
#> Warning: package 'readr' was built under R version 3.5.3
#> Warning: package 'purrr' was built under R version 3.5.3
#> Warning: package 'dplyr' was built under R version 3.5.3
#> Warning: package 'stringr' was built under R version 3.5.3
#> Warning: package 'forcats' was built under R version 3.5.3

# ensures you will get same values as i do
set.seed(123)


# create random data ------------------------------------------------------
random_response <- function() {
  responses <- c(
    "None",
    "A little of the time",
    "Sometime",
    "Most of the time",
    "All the time"
  )
  sample(responses, 5)
}

response_dat <- tibble(
  response_a = random_response(),
  response_b = random_response(),
  response_c = random_response(),
  response_d = random_response()
) %>%
  rownames_to_column()

response_dat
#> # A tibble: 5 x 5
#>   rowname response_a        response_b        response_c        response_d      
#>   <chr>   <chr>             <chr>             <chr>             <chr>           
#> 1 1       A little of the ~ None              All the time      All the time    
#> 2 2       Most of the time  Sometime          A little of the ~ None            
#> 3 3       All the time      Most of the time  Sometime          Most of the time
#> 4 4       Sometime          A little of the ~ Most of the time  Sometime        
#> 5 5       None              All the time      None              A little of the~

# convert to numeric ------------------------------------------------------
response_dat %>%
  mutate_at(vars(2:ncol(.)), list(~
  case_when(
    . == "None" ~ 0,
    . == "A little of the time" ~ 1,
    . == "Sometime" ~ 2,
    . == "Most of the time" ~ 3,
    . == "All the time" ~ 4,
    T ~ NA_real_
  )))
#> # A tibble: 5 x 5
#>   rowname response_a response_b response_c response_d
#>   <chr>        <dbl>      <dbl>      <dbl>      <dbl>
#> 1 1                1          0          4          4
#> 2 2                3          2          1          0
#> 3 3                4          3          2          3
#> 4 4                2          1          3          2
#> 5 5                0          4          0          1

Created on 2020-07-14 by the reprex package (v0.3.0)

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