[R] colMeans()

Jim Lemon jim at bitwrit.com.au
Tue Sep 29 04:57:06 CEST 2009


On 09/28/2009 10:47 PM, Matteo Mattiuzzi wrote:
> Hello,
>
> I use the function rowMeans(x,na.rm=T). The result is the mean of valid
> values in each row, with NA removed. A for me very important information
> is, from how many valid "n" this mean has computed.
>
> The thing is, that I apply this function on many millions of rows, so
> the time it takes is multiplied by this factor! (so getting this with
> "rowSums(is.na(x))" it takes much to long)
>
> I was trying to look inside the function maybe to get not only the
> means but the valid "n" too, but I wasn't able.
>
>    
Hi Matteo,
The number of observations that are NOT NA can be calculated with:

sum(!is.na(x))

and you can see this in action in the valid.n function in the prettyR 
package.

Jim




More information about the R-help mailing list