R - calculating time per unit using data.table -
thanks helping me out in advance.i have data looks this.
code , data:
zz <- "srno process username activityname units starttime 1 xyz usera activity1 2 '8/10/2017 11:40:37 am' 2 xyz usera activity1 2 '8/10/2017 11:41:39 am' 3 xyz usera activity1 2 '8/10/2017 11:42:41 am' 4 xyz usera activity2 3 '8/10/2017 11:43:44 am' 5 xyz usera activity2 3 '8/10/2017 11:49:37 am' 6 xyz usera activity1 2 '8/10/2017 11:54:21 am' 7 xyz usera activity1 2 '8/10/2017 11:58:21 am' 8 xyz usera activity2 4 '8/10/2017 11:59:21 am'" dt1 <- read.table(text= zz,header = true) library(lubridate) dt1$starttime <- mdy_hms(dt1$starttime) dt1$units<- as.numeric(dt1$units) dt1$endtime <- c(dt1$starttime[-1],dt1$starttime[length(dt1)]) dt1$timeinsecs <- dt1$endtime- dt1$starttime library(data.table) dt1<- data.table(dt1) calculatedsummary = dt1[,.(sum(timeinsecs)),by=.(process,username,activityname,units)] calculatedsummary$timeperunit <- calculatedsummary$v1/calculatedsummary$units
my expected output.
+ process username activityname units timeinsec timeperunit + xyz usera activity1 4 487 121.75 + xyz usera activity2 7 637 91.14
but instead getting output
+ process username activityname units v1 timeperunit + xyz usera activity1 2 487 243.5 + xyz usera activity2 3 637 212.33 + xyz usera activity2 4 0 0
can pls in getting faster database quite big , increasing on daily basis.
wiki
Comments
Post a Comment