I want to write a function that loads a data frame from my hard drive, and then creates a new dataframe that calculates the difference between column n and column n+4, and them saves this new dataframe to my hard drive, and finally, removes both the new and old data frame from memory.. Here is the code I am using. How do I convert this into a function that can be used to perform the same process on any dataframe? load ('c:/r_pit/sampledf.r') w<-ncol(sampledf) l<-nrow(sampledf) sampledf_yychg <- data.frame(matrix(data=NA,nrow=l,ncol=w-4)) for(j in 1:(w-4)) { sampledf_yychg[, j]<-sampledf[, j]- sampledf[, j+4] } save(sampledf_yychg, file='c:/r_pit/sampledf_yychg.r') rm(sampledf, sampledf_sq, sampledf_yychg)