# [R] Splitting times into groups based on a range of times

arun smartpink111 at yahoo.com
Fri Oct 11 16:50:33 CEST 2013

```Also,

df1 <- do.call(rbind,lapply(split(df,df\$group),function(x) data.frame(dt2=seq(x[1,1],x[nrow(x),1],by="15 min"),group=x[1,2])))
id1<- df1[,2][match(as.POSIXct(dt),df1[,1])]
id1[is.na(id1)]<- 0
identical(id1,id)
#[1] TRUE
A.K,

On Thursday, October 10, 2013 9:29 PM, arun <smartpink111 at yahoo.com> wrote:
Hi Ben,

I would look into ?findInterval() or ?cut() for an easier solution.
indx<- match(df[,1],as.POSIXct(dt))
indx2<- unique(df[,2])
lst1<- lapply(split(indx,((seq_along(indx)-1)%/%2)+1),function(x) seq(x[1], x[2]))
res <- unlist(lapply(seq_along(lst1),function(i) {
val<-rep(indx2[i],length(lst1[[i]]))
names(val)<-lst1[[i]]
val
}))
res1<-res[match(seq_along(dt),names(res))]
res1[is.na(res1)]<- 0
names(res1)<- NULL
res1
# [1] 1 1 1 2 2 2 3 3 3 0 0 4 4 4 4 4 4 4 4 4 4 4 4 0
identical(id,res1)
#[1] TRUE

On Thursday, October 10, 2013 8:10 PM, Benjamin Gillespie <gybrg at leeds.ac.uk> wrote:
Hi all,

I hope you can help with this one!

I have a dataframe: 'df' that consists of a vector of times: 'dt2' and a vector of group id's: 'group':

dates2=rep("01/02/13",times=8)
times2=c("12:00:00","12:30:00","12:45:00","13:15:00","13:30:00","14:00:00","14:45:00","17:30:00")
y =paste(dates2, times2)
dt2=strptime(y, "%m/%d/%y %H:%M:%S")
group=c(1,1,2,2,3,3,4,4)
df=data.frame(dt2,group)

I also have a vector: 'dt' which is a series of times:

dates=rep("01/02/13",times=20)
times=c("12:00:00","12:15:00","12:30:00","12:45:00","13:00:00","13:15:00","13:30:00","13:45:00","14:00:00","14:15:00","14:30:00","14:45:00","15:00:00","15:15:00","15:30:00","15:45:00","16:00:00","16:15:00","16:30:00","16:45:00","17:00:00","17:15:00","17:30:00","17:45:00")
x =paste(dates, times)
dt=strptime(x, "%m/%d/%y %H:%M:%S")

I wish to create a vector which looks like 'id':

id=c(1,1,1,2,2,2,3,3,3,0,0,4,4,4,4,4,4,4,4,4,4,4,4,0)

The rules I wish to follow to create 'id' are:

1. If a value in 'dt' is either equal to, or, within the range of times within group x in dataframe 'df', then, the value in 'id' will equal x.

So, for example, in 'df', group 4 is between the times of "14:45:00" and "17:30:00" on the "01/02/13". Thus, the 12th to 23rd value in 'id' equals 4 as these values correspond to times within 'dt' that are equal to and within the range of  "14:45:00" and "17:30:00" on the "01/02/13".

I'm not sure where to even start with this... possibly the 'cut' function?

o-------------------------------------------------------------------o
School of Geography, University of Leeds, Leeds, LS2 9JT
o-------------------------------------------------------------------o
Tel: +44(0)113 34 33345
Mob: +44(0)770 868 7641
o-------------------------------o
http://www.geog.leeds.ac.uk/
o-------------------------------------o
@RiversBenG
o--------------o
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help