R code for computing average signal