| To: | Stuart Luppescu <s-luppescu@uchicago.edu> |
|---|---|
| Subject: | Re: na.rm in matrix operations |
| From: | David L Lorenz <lorenz@usgs.gov> |
| Date: | Wed, 22 Jul 2009 13:24:28 -0500 |
| Cc: | s-news <s-news@lists.biostat.wustl.edu>, s-news-owner@lists.biostat.wustl.edu |
| In-reply-to: | <1248204240.31187.20.camel@musuko.spc.uchicago.edu> |
| References: | <1248204240.31187.20.camel@musuko.spc.uchicago.edu> |
|
Stuart, How about something like apply(b, 2, function(i,a) mean(a[i==1], na.rm=T), a=a) it is probably just a bit faster than the for loop. Dave
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. -------------------------------------------------------------------- This message was distributed by s-news@lists.biostat.wustl.edu. To unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with the BODY of the message: unsubscribe s-news |
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| ||
| Previous by Date: | SsfPack S+FinMetrics - Solving parameters for the Vasicek Model, Ellerton, Tim |
|---|---|
| Next by Date: | Refresh the memory, Mandans |
| Previous by Thread: | na.rm in matrix operations, Stuart Luppescu |
| Next by Thread: | SsfPack S+FinMetrics - Solving parameters for the Vasicek Model, Ellerton, Tim |
| Indexes: | [Date] [Thread] [Top] [All Lists] |