You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言中Likert量表重编码报错求助

解决李克特量表重编码的报错问题

问题根源

报错核心是调用的recode()函数与预期语法不匹配:大概率是加载了car包,其recode()函数覆盖了dplyr的同名函数,两者语法规则完全不同;另外第二段代码中的funs()已被dplyr废弃,不建议使用。

可行解决方案

方案1:明确使用dplyr的recode函数

在recode前指定dplyr::,避免包函数冲突:

rmydata.reloaded1 <- lik %>%
  mutate(across(c("Life Satisfaction Q1", "Life Satisfaction Q2", "Life Satisfaction Q3", "Life Satisfaction Q4"),
                ~ dplyr::recode(., "Disagree" = 1, "Neutral" = 2, "Agree" = 3, "Strongly agree" = 4)))

方案2:改用dplyr推荐的case_match函数(更直观)

case_match是dplyr新版本推荐的匹配替换函数,语法清晰不易出错:

rmydata.reloaded1 <- lik %>%
  mutate(across(c("Life Satisfaction Q1", "Life Satisfaction Q2", "Life Satisfaction Q3", "Life Satisfaction Q4"),
                ~ case_match(.,
                             "Disagree" ~ 1,
                             "Neutral" ~ 2,
                             "Agree" ~ 3,
                             "Strongly agree" ~ 4)))

方案3:若使用car包的recode,调整语法

如果确实需要用car包的recode,需按其语法规则编写(单引号包裹字符串,分号分隔规则):

library(car)
rmydata.reloaded1 <- lik %>%
  mutate(across(c("Life Satisfaction Q1", "Life Satisfaction Q2", "Life Satisfaction Q3", "Life Satisfaction Q4"),
                ~ recode(., "'Disagree'=1; 'Neutral'=2; 'Agree'=3; 'Strongly agree'=4")))

额外提示

  • 废弃的mutate_at()和funs()请替换为across(),这是dplyr当前的标准写法。
  • 可通过运行find("recode")检查当前recode函数所属的包,快速定位冲突来源。

内容的提问来源于stack exchange,提问作者Caesar Daras

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 17:45:06