0

This is my code:*

data %>%
  mutate(r_stemster_3 = recode(stemster_3,
                               `1` = 7,
                               `2` = 6, 
                               `3` = 5,
                               `4` = 4, 
                               `5` = 3,
                               `6` = 2,
                               `7` = 1))

This is the error message:

Error in `mutate()`:
ℹ In argument: `r_stemster_3 = recode(...)`.
Caused by error in `recode()`:
! unused arguments (`1` = 7, `2` = 6, `3` = 5, `4` = 4, `5` = 3, `6` = 2, `7` = 1)
Backtrace:
 1. data %>% ...
 3. dplyr:::mutate.data.frame(...)
 4. dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
 6. dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
 7. mask$eval_all_mutate(quo)
 8. dplyr (local) eval()

Error in mutate(., r_stemster_3 = recode(stemster_3, `1` = 7, `2` = 6,  : 
  
Caused by error in `recode()`:
! unused arguments (`1` = 7, `2` = 6, `3` = 5, `4` = 4, `5` = 3, `6` = 2, `7` = 1)

I can't figure out what was wrong with my code. Please help fix the code!

I tried variations of the code such as removing the backticks or using single/double quotes instead of backticks. None of them worked.

Also data$stemster_3 is numeric and I want the reverse coded variable to be numeric too.

6
  • 4
    8 - data$r_stemster_3 Commented Feb 29, 2024 at 16:14
  • 5
    Hi, I believe that you may have a package that changed the definition of the recode function. Either load the tidyverse package before running the code or replace recode with dplyr::recode. If you are running both the car and dplyr package, this is most likely the issue Commented Feb 29, 2024 at 16:14
  • 2
    You are right @Cravetheflame. The issue lies in the packages. After I removed car, the codes worked perfectly. Thank you so much! Commented Feb 29, 2024 at 16:26
  • 1
    Happy to help, but please consider @Onyambu's answer as it demonstrates a simpler way of dealing with what you were trying to do :) Commented Feb 29, 2024 at 16:29
  • Note more "scalable" extension of Onyambu's answer would be (max(data$r_stemster_3)+1) - data$r_stemster_3 Commented Feb 29, 2024 at 16:42

1 Answer 1

1

There are many ways to deal with reverse coding in R. Here are some:

  1. The simplest way
sampledf <- data.frame(V1 = c(1, 4, 5, 6, 7),
                       V2 = c(2, 4, 4, 5, 6),
                       V3 = c(2, 7, 1, 1, 5))
8 - sampledf
  1. 'psych' package
sampledf <- data.frame(V1 = c(1, 4, 5, 6, 7),
                       V2 = c(2, 4, 4, 5, 6),
                       V3 = c(2, 7, 1, 1, 5))

install.packages("psych")
library(psych)
reverseitems <- c(-1, 1, 1)
reverse.code(reverseitems, sampledf, mini = 1, maxi = 7)

note. reverseitems <- c(-1, 1, 1) is here to indicate V1 in the dataset. In this case, only the values in the first column will be reverse-coded.

  1. 'dplyr' package
sampledf <- data.frame(V1 = c(1, 4, 5, 6, 7),
                       V2 = c(2, 4, 4, 5, 6),
                       V3 = c(2, 7, 1, 1, 5))
library(dplyr)
sampledf |> 
  mutate(new = 8 - sampledf)

note. In this way, new variables will be added to the dataset (sampledf).

  1. apply() function
sampledf <- data.frame(V1 = c(1, 4, 5, 6, 7),
                       V2 = c(2, 4, 4, 5, 6),
                       V3 = c(2, 7, 1, 1, 5))
sample123 <- apply(sampledf[, c(1, 2, 3)], 2, function(x) 8-x)
sample123

note. By using c(1, 2, 3) you can specify the variables (eg. all variables). After having new dataset, you can merge the specific variables of the old and new datasets (data.frame() function).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.