[R] Add new calculated column to data frame

arun smartpink111 at yahoo.com
Thu Aug 29 20:03:13 CEST 2013



Hi,
Try:
dat1<- read.table(text="
id    event    time
1    add      1373502892
2    add      1373502972
3    delete  1373502995
4    view      1373503896
5    add      1373503996
",sep="",header=TRUE,stringsAsFactors=FALSE)
 dat1$time_on_task<- c(NA,diff(dat1$time))
 dat1
#  id  event       time time_on_task
#1  1    add 1373502892           NA
#2  2    add 1373502972           80
#3  3 delete 1373502995           23
#4  4   view 1373503896          901
#5  5    add 1373503996          100

#Not sure whether this depends on the values of "event" or not..
A.K.





----- Original Message -----
From: srecko joksimovic <sreckojoksimovic at gmail.com>
To: R help <R-help at r-project.org>
Cc: 
Sent: Thursday, August 29, 2013 1:52 PM
Subject: [R] Add new calculated column to data frame

Hi,

I have a following data set:
id    event    time (in sec)
1     add      1373502892
2     add      1373502972
3     delete   1373502995
4     view      1373503896
5     add       1373503996
...

I'd like to add new column "time on task" which is time elapsed between two
events (id2 - id1...). What would be the best approach to do that?

Thanks,
Srecko

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
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.




More information about the R-help mailing list