dataframe - R - Efficiency for calculating a column in a data frame -




i have couple of columns in data frame times. i'm trying calculate difference in times in new column, need reset 0 every time encounter start of new pattern. please see sample data below.

seq  atime      rt 0    18:33:00   0 20   18:48:00   15 43   19:01:00   13 56   19:47:00   24 0    21:33:00   0 9    21:45:00   12 22   21:55:00   10 45   22:13:00   18 0    06:33:00   0 22   06:47:00   14 45   06:59:00   12 62   07:22:00   23 85   07:48:00   26 

i'm using following script estimate delta column. seq column increasing each 'pattern'. in sample each pattern's seq starts 0, may not case always.

dat_4$rt <- 0 (i in 1:(nrow(dat_4$seq)-1)) {   if (dat_4$seq[i+1] > dat_4$seq[i]) {     dat_4$rt[i+1] = (chron(times=dat_4$atime[i+1]) - chron(times=dat_4$atime[i]))*1440   } else {     dat_4$rt[i+1] = 0   } } 

although works, it's not @ efficient. 'dat_4' dataframe have 4 million records , takes 2.5 minutes process step.

   user  system elapsed    96.86   54.07  150.99  

any suggestion on how can make more efficient?

you can first calculating rt rows, , finding of rows should set 0. avoids loop, , faster.

dat_4$rt <- c(0, diff(chron(times=dat_4$atime)) * 1440) dat_4$rt[which(sign(diff(dat_4$seq)) == -1) + 1] <- 0 

the first line diff using chron similar how did, avoids doing in loop. second line detects when seq has decreased, , sets rows have rt of zero.





wiki

Comments

Popular posts from this blog

elasticsearch - what is the equivalent data type for geo_point in hibernate search? -

Jenkins: find build number for git commit -

firebase - How to wait value in Ionic 2 -