【发布时间】:2014-03-02 22:46:52
【问题描述】:
我是 R 新手。我编写了一个适用于数字的函数,并希望将其应用于长度为 400 的数字。它去
EGIDS.to.IUCN <- function(x){
if(x==10){return(NA)} # 10 (Extinct)
if(x==9){return(NA)} # 9 (Dormant)
if(x==8.5){return(4)} # 8.5 (Nearly Extinct) → 4 (Critically endangered)
# 10 more similar lines here (no more NAs)
else{stop}
}
我尝试使用 lapply 但后来我得到了
> austroIUCN <- lapply(austroEGIDS, EGIDS.to.IUCN)
Error in if (x == 10) { : missing value where TRUE/FALSE needed
austroEGIDS 是从 0 到 10 的 400 个数字的列表。我完全迷路了。为什么在关闭 if 条件后需要一个布尔值?
【问题讨论】:
-
austroEGIDS是否包含缺失值 (NA)? -
stop语句应该做什么?如果不满足其中一个条件,您想要NA吗?因为如果您删除stop它应该可以工作
标签: r function if-statement lapply