简体   繁体   中英

R - Error in dplyr mutate recode when recoding factor variables "Caused by error in `recode()`: ! Argument 2 must be named."

I'm trying to recode some factor variables I have, for example:

Apples
Oranges
Cabbage
Broccoli
Cheese

What I'm looking to do is to recode them all so they become Fruit or Non_fruit.

If it is relevant at all, these were recoded earlier on in the script from numeric variables (eg 1 = Apples, 2 = Oranges, and so on. I used the exact same argument setup in this as I tried below, but did not get the error and it runs fine.

My argument currently looks like this:

df <- df %>%
dplyr::mutate(
x = recode(x, "Apples" = "Fruit", "Oranges" = "Fruit", .default = "Not_fruit")

The full error is

Error in dplyr::mutate(., diagnosis = recode(x, Apple = "Fruit", : 

ℹ The error occurred in row 1.
Caused by error in `recode()`:
! Argument 2 must be named.

I've also tried using recode_factor, and taking out the inverted commas around the variable (eg "Apple") but get the same result, or specifying each variable to recode, but get the same result again.

Would love any feedback or advice anyone may have on this please!

Try without " :

df <- df %>%
dplyr::mutate(
x = recode(x, Apples = "Fruit", Oranges = "Fruit", .default = "Not_fruit"))

Not teströed from mobile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM