[R] Inf and NA

R. Michael Weylandt michael.weylandt at gmail.com
Sat Jun 9 16:03:08 CEST 2012


Take a look at this:

x <- matrix(1:9, 3)
x[2,2] <- Inf
x[3,1] <- NA

rowMeans(x * is.finite(x), na.rm = TRUE)

I at first thought you could simply use x[is.finite(x)] but that
looses the matrix-ness of it so instead, we use the fact that 0 * Inf
= NaN which gets killed by na.rm = TRUE. Also see my notes inline
below.

Best
Michael

On Sat, Jun 9, 2012 at 6:00 AM, Trying To learn again
<tryingtolearnagain at gmail.com> wrote:
> Hi all,
>
> I have a csv matrix "KT.csv" and it has Inf and NA

Incidentally, this probably signals some confusion -- there's no such
thing as a "csv matrix" in R. If you read in a csv you will usually
(always?) get a data.frame which is most definitely not a matrix.


>
> I want to calculate the mean of each row so I use
>
> rowMeans(KT,na.rm = TRUE) but with this Inf cannot be omminted.

This was your best guess -- see above.

>
> I´m trying to use before running rowMeans(KT,na.rm = TRUE)
>
> KT<-range(KT,finite=TRUE)

This only replaces KT by its min and max loosing your data entirely.
Not at all what you were looking for.

>
> but it doesn´t works...
>
> Do you know a simple way to ommit Inf en the calculations?
>
> Many thanks.
>
> I have tried also to replace Inf by NA bt also I dind´n t get it....
>
> I feel....like a mule...
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list