[R] Cannot calculate mean() for double vector
Sarah Goslee
sarah.goslee at gmail.com
Fri Oct 16 20:06:13 CEST 2009
Are you sure that's the right solution? If you set them to 0, those values
are averaged in with the others, and that could make a substantial difference.
A much better approach:
mean(x_ema, na.rm=TRUE)
- see the help for mean for more information.
Sarah
On Fri, Oct 16, 2009 at 2:01 PM, Reuben Bellika <rubenyi at gmail.com> wrote:
> OK. It looks like I just have several NA values at the start of my array:
>
>> which (is.na(x_ema))
> [1] 1 2 3 4 5 6 7 8 9
>
> That make sense, because the moving average is not defined for those
> positions. I'll just have to set those values to zero:
>
>> x_ema = replace(x_ema, which(is.na(x_ema)), 0)
>> which (is.na(x_ema))
> integer(0)
>
> The mean() call works now and I can get on with my work. I'll have to
> remember to condition the data like this in the future.
>
> Thanks for the help!
>
> Reuben Bellika
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list