有人可以向我解释为什么解析为 NA 的逻辑评估会在基于向量比较的子集中产生虚假行吗?例如:
employee <- c("Big Shot CEO", "Programmer","Intern","Guy Who Got Fired Last Week")
salary <- c( 10000000, 50000, 0, NA)
emp_salary <- data.frame(employee,salary)
# how many employees paid over 100K?
nrow(emp_salary[salary>100000,]) # Returns 2 instead of 1 -- why?
emp_salary[salary>100000,]
# returns a bogus row of all NA's (not "Guy Who Got Fired")
# employee salary
# 1 Big Shot CEO 1e+07
# NA <NA> <NA>
salary[salary>100000]
# returns:
# [1] 1e+07 NA
NA > 100000 #returns NA
鉴于这种意外行为,在上述示例中计算收入超过 10 万的员工的首选方法是什么?