Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

dplyr - error message when using mutate function in r

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")))))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...