Skip to content

sub-setting rows based on dates in R

1 message · Jim Lemon

#
It looks to me as though the problem is in your input data. The following
example works for me:

df1<-data.frame(Date=paste(rep(6:8,each=20),
 c(sort(sample(1:30,20)),sort(sample(1:30,20)),sort(sample(1:30,20))),
 2016,sep="/"),
 Rainfall_Duration=sample(c(10,20,30,40),60,TRUE))
df1$Date<-as.Date(df1$Date,"%m/%d/%Y")
df2<-data.frame(Date=paste(rep(6:8,each=10),
 c(sort(sample(10:30,10)),sort(sample(10:30,10)),sort(sample(10:30,10))),
 2016,sep="/"),
 Removal.Rate=runif(30,10,60))
df2$Date<-as.Date(df2$Date,"%m/%d/%Y")

df3<-data.frame(Rate.Removal.Date=NULL,Date=NULL,Rainfall_Duration=NULL)

df3row<-0

for(i in 1:dim(df2)[1]) {
 rdrows<-which(df2$Date[i] >= df1$Date & !(df2$Date[i] > df1$Date + 8))
 # if there are no dates in df1 within the prior 7 days
 if(!length(rdrows)) {
  # first check if at least one date in df1 is less than the df2
  # date and is not included in the last set of df1 dates
  checkrows<-which(df2$Date[i] >= df1$Date)
  # use the last date greater than the maximum in lastrows
  if(any(checkrows > lastrows))
   rdrows<-max(checkrows[checkrows > lastrows])
  # otherwise use the last set
  else rdrows<-lastrows
 }
 # save the current set of dates
 lastrows<-rdrows
 # get the number of new rows
 nrows<-length(rdrows)
 for(row in 1:nrows) {
  # set the values in each row
  df3[row+df3row,1]<-format(df2$Date[i],"%m/%d/%Y")
  df3[row+df3row,2]<-format(df1$Date[rdrows[row]],"%m/%d/%Y")
  df3[row+df3row,3]<-df1$Rainfall_Duration[rdrows[row]]
 }
 df3row<-df3row+nrows
}

names(df3)<-c("Rate.Removal.Date","Date","Rainfall_Duration")
df3

Jim

On Fri, Feb 3, 2017 at 4:05 AM, Md Sami Bin Shokrana <samimist at live.com>
wrote: