[R] NAs and row/column calculations

Rolf Turner r.turner at auckland.ac.nz
Thu Mar 11 23:40:42 CET 2010


On 12/03/2010, at 11:25 AM, Jim Bouldin wrote:

> 
> I continue to have great frustrations with NA values--in particular making
> summary calculations on rows or cols of a matrix containing them.  For
> example, why does:
> 
>> a = matrix(1:30,nrow=5)
>> is.na(a[c(1:2),c(3:4)]);a
>     [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    1    6   NA   NA   21   26
> [2,]    2    7   NA   NA   22   27
> [3,]    3    8   13   18   23   28
> [4,]    4    9   14   19   24   29
> [5,]    5   10   15   20   25   30
>> apply(a[!is.na(a)],2,sum)
> 
> give me this:
> 
> "Error in apply(a[!is.na(a)], 2, sum) : dim(X) must have a positive length"
> 
> when
> 
>> dim(a)
> [1] 5 6
> 
> What is the trick to calculating summary values from rows or columns
> containing NAs?  Drives me nuts.  More nuts that is.

When you do a[!is.na(a)] you get a ***vector*** --- not a matrix.
``Obviously''!!!  The non-missing values of a cannot be arranged in
a 5 x 6 matrix; there are only 26 of them.  So (as my late Uncle
Stanley would have said) ``What the hell do you expect?''.

The ``trick'' is to remove the NAs at the summing stage:

apply(a,2,sum,na.rm=TRUE)

Not all that tricky.

	cheers,

		Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list