在R中使用mutate函数时出现错误消息 [英] error message when using mutate function in r
问题描述
收到以下错误消息:
mutate_impl(.data,点)中的错误:
评估错误:缺少参数 no,没有默认值。
receiving the following error message: Error in mutate_impl(.data, dots) : Evaluation error: argument "no" is missing, with no default.
mutate(x,perfLev= ifelse(SS< 1438, "Below Basic",
ifelse(SS>= 1439 & SS <= 1499, "Basic",
ifelse(SS >= 1500 & SS <= 1545, "Proficient",
ifelse(SS >= 1546, "Advanced")))))
推荐答案
使用Make212和Renu,这是修复它的一种选择:
Using comments by Make212 and Renu, here's one option for fixing it:
library(dplyr)
mutate(x,
perfLev = case_when(
SS < 1438 ~ "Below Basic",
SS >= 1439 & SS <= 1499 ~ "Basic",
SS >= 1500 & SS <= 1545 ~ "Proficient",
SS >= 1546 ~ "Advanced",
TRUE ~ "huh?"
) )
我添加了一个默认值( TRUE
),通常很好(显式代码)。请注意,如果您不包含 TRUE
,则它将获得 NA
值,以防万一, 。如果满足以下任何条件,我就会在这里看到它发生:
I added a "default" (TRUE
), which is generally good (explicit code). Note that if you do not include the TRUE
, then it would get an NA
value, in case that's what you want. I can see it happening here if any of the following are true:
-
is.na(SS)
-
SS> = 1438& SS < 1439
-
SS> 1499年SS < 1500
-
SS> 1545年SS < 1546
is.na(SS)
SS >= 1438 & SS < 1439
SS > 1499 & SS < 1500
SS > 1545 & SS < 1546
如果 NA
是可以接受的,并且您保证具有 SS
的完整性。
You may not need it if NA
is acceptable and you are guaranteed of SS
's integrality.
此代码相当于对您的代码进行了轻微修复:
This code is equivalent to a slight fix to your code:
mutate(x,
perfLev =
ifelse(SS < 1438, "Below Basic",
ifelse(SS >= 1439 & SS <= 1499, "Basic",
ifelse(SS >= 1500 & SS <= 1545, "Proficient",
ifelse(SS >= 1546, "Advanced", "huh?"))))
)
仅用于样式/清晰度的缩进。
Indentation for style/clarity only.
这篇关于在R中使用mutate函数时出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!