R is there a way to find Inf/-Inf values?

拜拜、爱过 提交于 2019-11-30 11:50:00
joran

You're probably looking for is.finite, though I'm not 100% certain that the problem is Infs in your input data.

Be sure to read the help for is.finite carefully about which combinations of missing, infinite, etc. it picks out. Specifically, this:

> is.finite(c(1,NA,-Inf,NaN))
[1]  TRUE FALSE FALSE FALSE
> is.infinite(c(1,NA,-Inf,NaN))
[1] FALSE FALSE  TRUE FALSE

One of these things is not like the others. Not surprisingly, there's an is.nan function as well.

randomForest's 'NA/NaN/Inf in foreign function call' is often a false warning, and really irritating:

  • you will get this if any of the variables passed is character
  • actual NaNs and Infs almost never happen in clean data

Fast and dirty trick to narrow things down, do a binary-search on your variable list, and use token parameters like ntree=2 to get an instant pass/fail on the subset of variables:

RF <- randomForest(prePrior1[m:n],ntree=2,...)
Paul Hiemstra

In analogy to is.na, you can use is.infinite to find occurrences of infinites.

Take a look at with, e.g.:

> with(df, df == Inf)
        foo   bar   baz   abc ...
[1,]  FALSE FALSE  TRUE FALSE ...
[2,]  FALSE  TRUE FALSE FALSE ...
...

joran's answer is what you want and informative. For more details about is.na() and is.infinite(), you should check out https://stat.ethz.ch/R-manual/R-devel/library/Matrix/html/is.na-methods.html and besides, after you get the logical vector which says whether each element of the original vector is NA/Inf, you can use the which() function to get the indices, just like this:

> v1 <- c(1, Inf, 2, NaN, Inf, 3, NaN, Inf)
> is.infinite(v1)
[1] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE
> which(is.infinite(v1))
[1] 2 5 8
> is.na(v1)
[1] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
> which(is.na(v1))
[1] 4 7

the document for which() is here https://stat.ethz.ch/R-manual/R-devel/library/base/html/any.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!