2

I am relatively new to R and currently doing an assignment where I have to run some models. I have some variables that I need to recode, but when running the code I get an error (see R code and error below).

I use library(dplyr) to execute the command. I have also tried putting ticks around the numbers, inserting dplyr::recode() and changed the order of the numbers, but still I get the same error.

What does this error mean and how can I fix it?

library(dplyr)
essdk <- essdk %>%
  mutate(relig2 = recode(rlgatnd,
                         "Every day" = 7,
                         "More than once a week" = 6,
                         "Once a week" = 5,
                         "At least once a month" = 4,
                         "Only on special holy days" = 3,
                         "Less often" = 2,
                         "Never" =  1,
                         "Refusal" = NULL,
                         "Don't know" = NULL,
                         "No answer" = NULL,
                         "NA's" = NULL))

Error: Problem with `mutate()` input `relig2`.
x unused arguments (`Every day` = 7, `More than once a week` = 6, `Once a week` = 5, `At least once a month` = 4, `Only on special holy days` = 3, `Less often` = 2, Never = 1, Refusal = NULL, `Don't know` = NULL, `No answer` = NULL, `NA's` = NULL)
ℹ Input `relig2` is `recode(...)`.
Backtrace:
2. dplyr::mutate(...)
 15. dplyr:::h(simpleError(msg, call))
Blundering Ecologist
  • 1,199
  • 2
  • 14
  • 38
Asiya Ahmed
  • 21
  • 1
  • 3
  • What does your input data actually look like? Does `environment(recode)` tell you that it's from `dplyr` or do you have other packages loaded as well. Also you can't have `NULL` values in a vector. Perhaps `NA` would be better in this case. – MrFlick Dec 18 '20 at 19:20
  • My input data is this right?: essdk <- read.dta("ESS1-9e01.dta"). environment(recode) tells me that it's from car, but I haven't loaded the package. And thanks for NA response! – Asiya Ahmed Dec 18 '20 at 19:23
  • Well, we have no idea what's in the file so it's hard to tell if it looks right. Also that' doesn't appear anywhere in the `mutate` command. Are you piping that data into the function somewhere in code that you didn't include here? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 18 '20 at 19:24
  • Well, something loaded `car` for you. Use `dplyr::recode()` rather than just `recode()` to make sure you are using the `dplyr` version of the function. – MrFlick Dec 18 '20 at 19:25
  • Oh sorry! I just saw that my post didn't include the first part which is `essdk <- essdk %>% mutate(relig2 = recode(rlgatnd,....` The variable rlgatnd is a factor with 10 levels. Also I detached `car` and get a new error code saying: `Error: Problem with `mutate()` input `relig2`. x 'recode': all recodings should be formula but: 7 ℹ Input `relig2` is `recode(...)`` – Asiya Ahmed Dec 18 '20 at 19:33
  • Wait, it worked now that I tried your suggestion with `dplyr::recode()`. I feel a little stupid now, but thanks so much for helping! – Asiya Ahmed Dec 18 '20 at 19:35

0 Answers0