我有如下数据:
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.frame
或data.table
?
也许
which(is.na(as.numeric(unlist(dat))) != is.na(dat))