Re: [R] calculating mean excluding zeros

2011-07-19 Thread Sarah Goslee
On Tue, Jul 19, 2011 at 5:20 PM, Dimitri Liakhovitski wrote: > Thanks a lot, Sarah. > I assume, if the values against which I am comparing are REALLY zero > ("0") - then even the first one (mean(testvec[testvec != 0])) should > work, right? > Dimitri Well, yes. But what's "really" zero? > ((.2 +

Re: [R] calculating mean excluding zeros

2011-07-19 Thread Bert Gunter
Sarah et. al: On Tue, Jul 19, 2011 at 1:56 PM, Sarah Goslee wrote: > In the more general case, that approach is prone to machine precision > error (FAQ 7.31). > > Here's a clunky but safer alternative: > Perhaps ?zapsmall . However, I would agree with your sentiments that it may depend on conte

Re: [R] calculating mean excluding zeros

2011-07-19 Thread Dimitri Liakhovitski
Thanks a lot, Sarah. I assume, if the values against which I am comparing are REALLY zero ("0") - then even the first one (mean(testvec[testvec != 0])) should work, right? Dimitri On Tue, Jul 19, 2011 at 4:56 PM, Sarah Goslee wrote: > In the more general case, that approach is prone to machine pr

Re: [R] calculating mean excluding zeros

2011-07-19 Thread Sarah Goslee
In the more general case, that approach is prone to machine precision error (FAQ 7.31). Here's a clunky but safer alternative: > set.seed(1234) > testvec <- sample(0:10, 100, replace=TRUE) > mean(testvec) [1] 4.31 > mean(testvec[testvec != 0]) [1] 4.842697 > mean(testvec[!sapply(testvec, function

Re: [R] calculating mean excluding zeros

2011-07-19 Thread Weidong Gu
You can do it by subsetting or indexing r<-c(0,0,0,rnorm(10,10,5)) > mean(r) [1] 8.052215 > mean(r[r!=0]) [1] 10.46788 Weidong Gu On Tue, Jul 19, 2011 at 4:36 PM, Dimitri Liakhovitski wrote: > Sorry if it's been discussed before - don't seem to find it. > I'd like to calculate a mean while ign

[R] calculating mean excluding zeros

2011-07-19 Thread Dimitri Liakhovitski
Sorry if it's been discussed before - don't seem to find it. I'd like to calculate a mean while ignoring zeros. "mean" doesn't seem to have an option for that. Any other function/package that could do it? Thanks for a pointer! -- Dimitri Liakhovitski marketfusionanalytics.com __