r - Grouping based on measurement datetime continuity -
r - Grouping based on measurement datetime continuity -
my problem has grouping measurements based on continuity. here example.
a=seq(as.posixct("2014-07-20 10:00:00"), as.posixct("2014-07-20 12:00:00"), by="30 min") b=seq(as.posixct("2014-07-20 20:00:00"), as.posixct("2014-07-20 22:00:00"), by="30 min") c=seq(as.posixct("2014-07-21 08:30:00"), as.posixct("2014-07-21 10:30:00"), by="30 min") df= data.frame(date=c(a,b,c), conc=runif(15))
the desired output this
date conc grouping 2014-07-20 10:00:00 0.30899449 x 2014-07-20 10:30:00 0.25436235 x 2014-07-20 11:00:00 0.01122904 x 2014-07-20 11:30:00 0.38944058 x 2014-07-20 12:00:00 0.26457760 x 2014-07-20 20:00:00 0.50039528 y 2014-07-20 20:30:00 0.72761115 y 2014-07-20 21:00:00 0.06544978 y 2014-07-20 21:30:00 0.01836020 y 2014-07-20 22:00:00 0.26401722 y 2014-07-21 08:30:00 0.51394754 z 2014-07-21 09:00:00 0.23298657 z 2014-07-21 09:30:00 0.27799685 z 2014-07-21 10:00:00 0.34541882 z 2014-07-21 10:30:00 0.08069711 z
try
df$group <- c("x", "y", "z")[cumsum(c(true,diff(df$date)!=30))] df$group #[1] "x" "x" "x" "x" "x" "y" "y" "y" "y" "y" "z" "z" "z" "z" "z"
or
df$group <- factor(cumsum(c(true,df$date[-1]-df$date[-nrow(df)] >30)), labels=letters[24:26])
r datetime group
Comments
Post a Comment