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

Benjamin Gillespie gybrg at leeds.ac.uk
Fri Oct 11 23:43:34 CEST 2013


Hi Arun,

Many thanks for that solution; it works well :)

I also have some data where times and dates don't follow a 15 min intervals, so, for example:

dates=rep("01/02/13",times=20)
times=c("12:03:50","12:15:32","12:29:08","12:45:09","13:01:00","13:14:06","13:30:20","13:47:00","13:58:00","14:15:02","14:30:03","14:46:00","15:02:00","15:15:30","15:32:50","15:45:04","16:00:04","16:15:60","16:30:03","16:45:04","17:00:50","17:15:03","17:28:06","17:45:00")
x =paste(dates, times)
dt=strptime(x, "%m/%d/%y %H:%M:%S")

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)

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)
final.df=data.frame(dt,id)

Would you be able to suggest any workarounds for a situation like this?

Many thanks in advance,

Ben Gillespie, Research Postgraduate
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
________________________________________
From: arun [smartpink111 at yahoo.com]
Sent: 11 October 2013 02:29
To: Benjamin Gillespie
Cc: R help
Subject: Re: [R] Splitting times into groups based on a range of times

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".

If this doesn't make sense, please ask,

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

Many thanks in advance,

Ben Gillespie, Research Postgraduate
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
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list