s-news
[Top] [All Lists]

na.rm in matrix operations

To: s-news <s-news@lists.biostat.wustl.edu>
Subject: na.rm in matrix operations
From: Stuart Luppescu <s-luppescu@uchicago.edu>
Date: Tue, 21 Jul 2009 14:24:00 -0500
Hello, I have a numeric vector of length 2.6 million (call it a), and a
matrix of {0, 1} that is 2.6 million x 40 (call it b). I want to get the
a mean of the values in the vector that correspond to 1s for each of the
columns of the matrix. I thought the easiest and most efficient way to
do it would be like this:

t(a) %*% b / apply(b, 2, sum)

The problem is that there are NAs in the vector, so I get NA for every
mean. I could do it like this

for(i in 1:40) 
   mean(a[b[,i]==1], na.rm=T)

but that seems very inefficient. Is there some way to exclude NAs from
matrix calculations like this?

(I'm doing this in R 2.9.1 in Linux.)

Thanks.
-- 
Stuart Luppescu -=- slu .at. ccsr.uchicago.edu        
University of Chicago -=- CCSR 
才文と智奈美の父 -=-    Kernel 2.6.28-gentoo-r5                
Xander: So, Buffy, how'd the slaying go last night?
 Buffy: Xander!  Xander: I mean, how'd the laying
 go? No, I don't mean that either. 
 
 
 
 
 
 

<Prev in Thread] Current Thread [Next in Thread>