I have 5five POSIXct
type vectors. ptime
vector is the reference vector. I want to find matching dates between ptime
and the rest of the vectors. Once a date is matched then I want to perform a time comparison. A time comparison is followed and the the results are populated in a data.frame(test)
with an appropriate classifying number.
# create the reference and the other vectors
ptime <- sample(seq(as.POSIXct('2005-08-01'),as.POSIXct('2006-05-31'), by='hour'),1051)
dawn <- sample(seq(as.POSIXct('2005-01-01'),as.POSIXct('2007-12-31'),by='hour'),1095)
sunrise <- sample(seq(as.POSIXct('2005-01-01'),as.POSIXct('2007-12-31'),by='hour'),1095)
sunset <- sample(seq(as.POSIXct('2005-01-01'),as.POSIXct('2007-12-31'),by='hour'),1095)
dusk <- sample(seq(as.POSIXct('2005-01-01'),as.POSIXct('2007-12-31'),by='hour'),1095)
# extract the date to compare using only the `dawn` vector
# all other vectors (except ptime) have the same date and length
pt <- as.Date(ptime)
dw <- as.Date(dawn)
# create data.frame
time <- c(1:1051)
test<-data.frame(time)
# I use a data.frame because I want to re-populate an existing data.frame
> str(test)
'data.frame': 1051 obs. of 1 variable:
$ time: int 1 2 3 4 5 6 7 8 9 10 ...
# this is the loop that matches and assigns
for( b in 1:length(ptime) ){
for( a in 1:length(dawn) ) {
if( dw[a] == pt[b] ){
if( ptime[b] < dawn[a] ) {
test$time[b] <- 1
}else if( ptime[b] < sunrise[a] ) {
test$time[b] <- 2
}else if( ptime[b] < sunset[a] ) {
test$time[b] <- 3
}else if( ptime[b] < dusk[a] ) {
test$time[b] <- 4
}else
test$time[b] <- 1
}
}
}
# output result shows the categorization sequence of 1, 2, 3, and 4
> head(test)
time
1 1
2 1
3 3
4 1
5 1
6 3
The above code accomplishes what I want to do... but it takes 98.58
seconds. I have more data that varies in length (up to 5000).
Since I am a newbie to this, my guess is... what is taking so much time is the comparison of the DATES. Every time a new comparison has to be made dw[a] == pt[b]
the process must search through dw[a]
. Also, are the if-else
statements necessary to accomplish the task?
Can anyone provide a faster/more efficient method to loop
through, find matches, and store the results?
Greatly appreciate it. Thanks