[R] Splitting times into groups based on a range of times
Benjamin Gillespie
gybrg at leeds.ac.uk
Fri Oct 11 00:35:29 CEST 2013
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
More information about the R-help
mailing list