# Create simple function to average column over different ranges of rows

#### sarahgrogan

##### New Member
I've learned how to subset a group of rows I am interested in from a matrix, and average those values. The data set shows the percent of light reflected at many different wavelengths (rows).

If I want to know the average amount of light reflected at ~900 nm, I use the following code to average several different wavelengths (rows) together for each column:

data.means900 <- colMeans(subset(Data,Data$Wavelength > 899.5 & Data$Wavelength <900.5))

However, I don't know how to create functions. I would like to be able to create a function that when I ask for the average amount of light reflected at X nm, it averages X +- 0.5 nm. So, asking for 900 nm would average 899.5 : 900.5, asking for 950 would average between 949.5 : 950.5, etc.