Q:

Using R to find difference between two visits

I have data frame like this
vehicleId visitDate taskName
123       1/1/2013  Change Battery
456       1/1/2013  Wiper Blades Changed
123       1/2/2013  Tire Pressure Check
123       1/3/2013  Tire Rotation
456       3/1/2013  Tire Pressure Check

I want to do is
vehicleId visitDate timeBetweenVisits(hrs)
123        1/1/2013                     24
123        1/2/2013                    672
456        1/1/2013                     48

Any thoughts how I can do this using R?  

A:

With res as in @Dirk's answer, here is a by expression that does the job:
by(res, res$vehicleId, FUN=function(d)
                       {
                         data.frame(vehicleId=head(d$vehicleId, -1),
                                    visitDate=head(d$visitDate, -1), 
                                    tbv=diff(d$visitDate)) 
                       }
)
## res$vehicleId: 123
##   vehicleId  visitDate    tbv
## 1       123 2013-01-01 1 days
## 2       123 2013-01-02 1 days
## ---------------------------------------------------------------------------------------------- 
## res$vehicleId: 456
##   vehicleId  visitDate     tbv
## 1       456 2013-01-01 59 days

