Another option Daily$tmdiff <- with(Daily, c(NA, diff(Date, units='days')))
Hope this is helpful, Dan Daniel Nordlund, PhD Research and Data Analysis Division Services & Enterprise Support Administration Washington State Department of Social and Health Services > -----Original Message----- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Morway, > Eric > Sent: Thursday, May 26, 2016 5:00 PM > To: R mailing list > Subject: [R] rollapply and difftime > > Technically, the code below works and results in a column that I'm interested > in working with for further processing. However, it is both inefficient on > lengthy (>100 yr) daily time series and is, frankly, not the R way of doing > things. Using the 'Daily' data.frame provided below, I'm interested to know > the propeR way of accomplishing this same task in an efficient manner. I > tried combinations of rollapply and difftime, but was unsuccessful. Eric > > Daily <- read.table(textConnection(" Date Q > 1911-04-01 4.530695 > 1911-04-02 4.700596 > 1911-04-03 4.898814 > 1911-04-04 5.097032 > 1911-04-05 5.295250 > 1911-04-06 6.569508 > 1911-04-07 5.861587 > 1911-04-08 5.153666 > 1911-04-09 4.445745 > 1911-04-10 3.737824 > 1911-04-11 3.001586 > 1911-04-12 3.001586 > 1911-04-13 2.350298 > 1911-04-14 2.661784 > 1911-04-16 3.001586 > 1911-04-17 2.661784 > 1911-04-19 2.661784 > 1911-04-28 3.369705 > 1911-04-29 3.001586 > 1911-05-20 2.661784"),header=TRUE) > > Daily$Date <- as.Date(Daily$Date) > Daily$tmdiff <- NA > for(i in seq(2,length(Daily$Date),by=1)){ > Daily$tmdiff[i] <- as.numeric(difftime(Daily$Date[i],Daily$Date[i-1])) > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.