Calculating moving average in R

When the raw data obtained from an experiment is too noisy and you need to smooth-en it to better represent the trend, you need to calculate the moving average . Moving average is nothing but average of n previous numbers, with a specific step size.  Let me give an example:  if there are 100 numbers, then moving average is calculated by averaging 1-15, 2-16, 3-17 and so on.  Here, the n is 15 and step size is 1.

 

The R script to do this:

datain <- read.table("input.txt", header=1)
field2 = datain[,2]
coef15 = 1/15
mvavg15 = filter(field2, rep(coef15, 15), sides=1)
jpeg('input.jpg')
plot(mvavg15, type="l", main="Plot Title", xlab="X label", ylab="Y label")
dev.off()

Here, the data is assumed to be 2 column, first with serial number and second with value.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s