【问题标题】:R: Error while recoding variables using car::recode functionR:使用 car::recode 函数重新编码变量时出错
【发布时间】:2018-12-06 14:16:32
【问题描述】:

我经常使用库(汽车)中的“重新编码”功能来重新编码变量中的级别。直到今天我的代码都运行良好,但现在它给我带来了错误。 df 等没有任何变化,不确定发生了什么。
也许有人可以启发我!

我的数据框(示例)

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")

我的实际数据集中的级别:-

    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 

我的代码(直到今天都运行良好!):-

# 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: Argument 2 must be named, not unnamed

【问题讨论】:

    标签: r data-manipulation recode r-car


    【解决方案1】:

    它对我来说是一个干净的会话。我猜car::recode()dplyr::recode() 冲突。限定功能是否有效?将recode 替换为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'")
    

    当我明确调用 dplyr::recode() 时,我收到您的错误“错误:参数 2 必须命名,而不是未命名”。

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

    【讨论】:

    • 谢谢你,效果很好!我不能投票给你的答案,因为我的声誉很低,否则是一个快速简单的解决方案。再次感谢!
    猜你喜欢
    • 2019-01-08
    • 2022-01-23
    • 2015-06-26
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多