[R] moving average and NA values

From: Cornelis de Gier <cwdegier_at_gmail.com>
Date: Mon, 10 Dec 2007 12:45:35 +0100


The S-plus function moving.ave(data, span = 2) calculates the moving average, but it does not have an argument to tell it how to deal with NA values, so it will return NA for all averages as shown below.

Is there an R or S moving average function which is able to omit some NA values in the dataset?

In the simple sample shown below it would be possible to just remove the rows with NA values. The dataset on which I want to use the moving average function with a span of 270 is a time series dataset, just removing rows would corrupt this dataset and make it unfit to plot.

Cornelis

t <- (1:10)
moving.ave(t,2)
$aves:

 [1] 1.0 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5

$sizes:

 [1] 1 2 2 2 2 2 2 2 2 2

t[5] <- NA
moving.ave(t,2)
$aves:

 [1] NA NA NA NA NA NA NA NA NA NA

$sizes:

 [1] 1 2 2 2 2 2 2 2 2 2



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 10 Dec 2007 - 11:52:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 10 Dec 2007 - 16:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.