[R] Add new calculated column to data frame

Berend Hasselman bhh at xs4all.nl
Thu Aug 29 20:34:00 CEST 2013


On 29-08-2013, at 20:15, srecko joksimovic <sreckojoksimovic at gmail.com> wrote:

> Thanks Arun,
> 
> this is great. However, it should be just a little bit different:
> 
> #  id  event       time time_on_task
> #1  1    add 1373502892           80
> #2  2    add 1373502972           23
> #3  3 delete 1373502995           901
> #4  4   view 1373503896          100
> #5  5    add 1373503996          NA
> 
> When I calculate difference, I need to know how long each activity was. It
> is id2-id1 for the first activity...

then why don't you try

dat1$time_on_task<- c(diff(dat1$time),NA)

Berend



More information about the R-help mailing list