我怎么知道强制使用NA是什么?

时间:2019-08-12 21:41:45

标签: r validation na

我需要清理一些混乱的数据集。某些操作通过强制引入了NA,但是即使没有,数据集也包含许多NA。如何确定引入了NA的行或元素。

例如

a <- c(1,2,"three", rep(NA, times=10))
as.numeric(a)
 [1]  1  2 NA NA NA NA NA NA NA NA NA NA NA
Warning message:
NAs introduced by coercion 

导致将第三个元素强制为数字。有没有办法确定是造成此问题的第三个因素,而不是其他NA(非)值?谢谢!

1 个答案:

答案 0 :(得分:4)

尝试

which(is.na(as.numeric(a)) != is.na(a))
3
# Warning message:
# In which(is.na(as.numeric(a)) != is.na(a)) : NAs introduced by coercion