-2

I have uber dataset containing variables pickup point, request time, drop time, date variable without month and year.

I need code for calculating idle time and creating a new variable idle time. Calculation as follows:

If pickup points are same for consecutive rows and date is different for consecutive rows then NA value if not difference between drop time of first row and the pickup time in second row. I have done it in excel and need to do it in R

Attached is the screenshot of data in excel

enter image description here

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • Please provide a reproducible example and the R code you have tried so far. – Adam Quek Jun 01 '17 at 05:59
  • Please have a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so that it is easy for others to help you. – Ronak Shah Jun 01 '17 at 06:04
  • I am very new to programming hence I have no idea how to write it in R – Kruthik Jun 01 '17 at 06:05

1 Answers1

-1

Try something like this, if this is what you are looking for

for(i in 2:nrow(df)){
  df$idle[1]<-NA
  if(df$Pickup.point[i]!=df$Pickup.point[i-1])
    df$idle[i]<-NA
  else
    if(df$Date[i]!=df$Date[i-1])
      df$idle[i]<-NA
    else
      df$idle[i]<-(df$Req[i]-df$Drop[i-1])
}
Ishan Juneja
  • 401
  • 4
  • 11