Thank you all for your help. It's working now. I chose to use the sqldf method and fn$ in the gsubfn package. I used fn$ so that I could put variables into the sqldf statement. This helped me to increase or decrease the window size in the Gene dataframe if I wanted to include values in the average both before or after the gene.
For anyone else interested, this is what it looked like. Avg_func<-function(chr,begin,finish){ fn$sqldf(paste("select d1C",chr,".'ORF', avg(C0), avg(C1) from d1C",chr,", d2C",chr," where d2C",chr,".Pos between d1C",chr,".Start-",begin," and d1C",chr,".End+",finish," group by d1C",chr,".'ORF'",sep="")) } I used d1C1 etc. because I had a different file for each chromosome that I read into separate dataframes in R. The dataframes also had ORF (Open Reading Frame) instead of Group. -- View this message in context: http://r.789695.n4.nabble.com/Averaging-within-a-range-of-values-tp4291958p4302910.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.