我有一个df,应该是所有的数字,但都是字符输入。我正在尝试这个:
sapply(df, as.numeric)
会抛出警告:
There were 50 or more warnings (use warnings() to see the first 50)
我可以忽略这些警告,但我想找出问题所在。如何找到哪些列正在抛出警告?
这看起来相当基本,但我无法弄清楚如何获得它。
如果有帮助:
test <- as.data.frame(list(c("1","2","3"), c("1","poop","3")))
> sapply(test, as.numeric)
c..1....2....3.. c..1....poop....3..
[1,] 1 1
[2,] 2 NA
[3,] 3 3
Warning message:
In lapply(X = X, FUN = FUN, ...) : NAs introduced by coercion
我想要的是一个功能,告诉我第2列是有问题的。
答案 0 :(得分:0)
感谢@RichScriven。
这有效:
test2 <- sapply(test, function(x) tryCatch(as.numeric(x), warning = function(w) "This one!!!"))
which(test2 == "This one!!!")