[R] Cannot calculate mean() for double vector

Alberto Monteiro albmont at centroin.com.br
Fri Oct 16 19:39:34 CEST 2009


Reuben Bellika wrote:
>
> I've been using R recently to analyze some data, but I'm having a
> problem using the mean() function.
> 
> I imported the original data set as a vector of integers, x and then
> calculated a exponential moving average of the data, x_ema. This part
> worked fine.
> 
> Then, I tried to find the mean squared error between the original
> series and the moving average, using mse = mean((x - x_ema)^2). This
> gives N/A as a result, which seems to be the result of the mean
> function. When I run mean() on x_ema, which is of data type double,
>  it always returns N/A. 
>
So, x_ema includes one (or more) NA (and not N/A) in it.

Test: if (any(is.na(x_ema))) cat("Oops! NAs in x_ema\n")

If you want to get which of them are na: which(is.na(x_ema))

Alberto Monteiro




More information about the R-help mailing list