一个矢量I有两个向量分配等级基于多个条件
vThresholds = as.vector(c(0.12, 0.34, 0.56, 0.85))
vCandidates = as.vector(rnorm(100))
欲基于它们位于vThresholds其中分配等级的候选者。我写了一个函数如下
fGrades = function (x, y) {
if(y <= x[1]){
grade = "A"
} else if(y > x[1] & y <= x[2]){
grade = "B"
} else if(y > x[2] & y <= x[3]){
grade = "C"
} else if(y > x[3] & y <= x[4]){
grade = "D"
}else {grade = "E"}
grade
}
然而,当我使用
mapply(fGrades, vThresholds, vCandidates)
我得到一个错误
Error in if (y <= x[1]) { : missing value where TRUE/FALSE needed
在跟踪这个错误我发现mapply覆羽X为NA。
我可以绕过vCandidates来解决这个问题。但是,我正在寻找此解决方案的矢量化版本。有没有更简单的方法来做到这一点?
谢谢joran和@DWin。 – earthlink 2013-05-01 23:40:24
我想给我支票,然后切换到乔兰是_just_权。显示你得到了两个消息,但与更好的一个。 – 2013-05-02 04:36:46