r - Calculating time spent on categories -
i have category string follows:
categoryvector <- c("1_100_1_2_3")
i have time corresponding each category:
timevector <- c("2013-03-07 05:16:50,617_2013-03-07 05:19:24,984_2013-03-07 05:21:06,002_2013-03-07 05:21:06,833_2013-03-07 05:21:10,713")
i calculate time spent on categories 1 , 2
time spent in category 1: (time in 100 - time in 1) + (time on 2 - time on 1) time spent in category 2: time on 3 - time on 2
i need repeat these calculations 200k+records. there efficient way in r?
inp <- read.table(text=gsub("_", "\n", timevector), sep=",") inp$v1 <- as.posixct(inp$v1) inp2 <- read.table(text=gsub("_", "\n", categoryvector)) inp$diffs <- c( difftime(inp$v1[-1], inp$v1[-nrow(inp)]), na) inp <- cbind(inp,inp2) v1 v2 diffs v1 1 2013-03-07 05:16:50 617 154 1 2 2013-03-07 05:19:24 984 102 100 3 2013-03-07 05:21:06 2 0 1 4 2013-03-07 05:21:06 833 4 2 5 2013-03-07 05:21:10 713 na 3 # should rename columns tapply(inp$diffs, inp[,4], sum, na.rm=true) # 1 2 3 100 #154 4 0 102
Comments
Post a Comment