r语言 - 从数据帧获取通过强制引入的 NA



我有如下数据:

library(data.table)
dat <- fread("A B C
1 2 4
1 1 1
one 3 5")

当将整个data.frame转换为数字时,我想知道哪些NA是通过强制引入的。

看到这个问题的答案,我尝试了:

which(is.na(as.numeric(dat)) != is.na(dat))

但这给出了一个错误:

Error in h(simpleError(msg, call)) : 
error in evaluating the argument 'x' in selecting a method for function 'which': 'list' object cannot be coerced to type 'double'

我也试过:

for (i in seq_along(dat)) {
which(is.na(as.numeric(dat[,i])) != is.na(dat[,i]))
}

结果:

Error in h(simpleError(msg, call)) : 
error in evaluating the argument 'x' in selecting a method for function 'which': j (the 2nd argument inside [...]) is a single symbol but column name 'i' is not found. Perhaps you intended DT[, ..i]. This difference to data.frame is deliberate and explained in FAQ 1.1.

最后:

for (i in seq_along(dat)) {
which(is.na(as.numeric(dat[,..i])) != is.na(dat[,..i]))
}

我再次得到:

Error in h(simpleError(msg, call)) : 
error in evaluating the argument 'x' in selecting a method for function 'which': 'list' object cannot be coerced to type 'double'

如何将此解决方案应用于data.framedata.table

也许

which(is.na(as.numeric(unlist(dat))) != is.na(dat))

最新更新