简体   繁体   中英

R: Error while recoding variables using car::recode function

I frequently use 'recode' function in library(car) to recode levels in variables. My code was working fine up until today, but now it is throwing me error. Nothing's changed in df etc, not sure what's going on.
May be somebody could enlighten me!

My Dataframe(sample) :

test<-structure(list(Avg.Salary = c("65000", "395", "82000", "128357", 
"95785", "95785"), Education = c("Doctorate", "Professional Degree", 
"Bachelor's", "Professional Degree", "Master's", "Master's"), 
Count = c("D", "D", "D", "D", "D", "364584"), Year = c(2017, 
2017, 2017, 2017, 2017, 2017)), row.names = c("540061", "540071", 
"540081", "540091", "540102", "540112"), class = "data.frame")

Levels in my actual dataset :-

    Associate Degree           Associates           Bachelor's 
             205                   35                42446 
               D            Doctorate          High School 
           42902                 9846                  191 
        Master's    Missing Education           No Diploma 
           57644                  218                   79 
    Professional  Professional Degree         Some College 
             431                 6791                   60 
 Some College Credits 
             370 

My Code(that was working fine till today!) :-

# Recode the education levels
test$Education<-recode(test$Education,
                 "c('Associate Degree','Associates','D','High School',
                    'No Diploma','Missing Education',
                    'Professional','Professional Degree','Some College',
                    'Some College Credits')='Others'")

Error :- Error: Argument 2 must be named, not unnamed

It works in a clean session for me. I'm guessing the car::recode() is conflicting with dplyr::recode() . Does qualifying the function work? Replace recode with car::recode .

test$Education <- car::recode(test$Education,
                 "c('Associate Degree','Associates','D','High School',
                    'No Diploma','Missing Education',
                    'Professional','Professional Degree','Some College',
                    'Some College Credits')='Others'")

When I call dplyr::recode() explicitly, I get your error of "Error: Argument 2 must be named, not unnamed".

test$Education <- dplyr::recode(...)

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