[R] start and end times to yes/no in certain intervall

Allan Engelhardt allane at cybaea.com
Fri Jul 23 15:17:44 CEST 2010


I like loops for this kind of thing so here is one:

df<- structure(list(start = c("15:00", "15:00", "15:00", "11:00",
"14:00", "14:00", "15:00", "12:00", "12:00", "12:00", "12:00",
"12:00", "12:00", "12:00", "12:00", "12:00", "12:00", "12:00",
"12:00", "12:00"), end = c("16:00", "16:00", "16:00", "12:00",
"16:00", "15:00", "16:00", "13:00", "13:00", "13:00", "13:00",
"13:00", "13:00", "13:00", "13:00", "13:00", "13:00", "13:00",
"13:00", "13:00")), .Names = c("start", "end"), row.names = c(NA,
20L), class = "data.frame")

duration<- with(df, floor(
                       as.numeric(
                        difftime(strptime(end, format="%H:%M"),
                                 strptime(start, format="%H:%M"),
                                 units = "hours"))))

start<- as.integer(substr(df$start, 1, 2))

a<- matrix(FALSE, nrow = NROW(df), ncol = 24,
             dimnames = list(1:NROW(df), paste("t", 0:23, sep = "")))
for (i in 1:NROW(df)) {
     names<- paste("t", seq(start[i], by = 1L, length.out = duration[i]), sep = "")
     a[i, names]<- TRUE
}
r<- range(which(apply(a, 2, any)))
a<- a[, r[1]:r[2]]  #  Drop columns we do not need
head(a)
#     t11   t12   t13   t14   t15
# 1 FALSE FALSE FALSE FALSE  TRUE
# 2 FALSE FALSE FALSE FALSE  TRUE
# 3 FALSE FALSE FALSE FALSE  TRUE
# 4  TRUE FALSE FALSE FALSE FALSE
# 5 FALSE FALSE FALSE  TRUE  TRUE
# 6 FALSE FALSE FALSE  TRUE FALSE



Hope this helps a little.

Allan


On 23/07/10 11:02, Stefan Uhmann wrote:
> Hi List,
>
> I have start and end times of events
>
> structure(list(start = c("15:00", "15:00", "15:00", "11:00",
> "14:00", "14:00", "15:00", "12:00", "12:00", "12:00", "12:00",
> "12:00", "12:00", "12:00", "12:00", "12:00", "12:00", "12:00",
> "12:00", "12:00"), end = c("16:00", "16:00", "16:00", "12:00",
> "16:00", "15:00", "16:00", "13:00", "13:00", "13:00", "13:00",
> "13:00", "13:00", "13:00", "13:00", "13:00", "13:00", "13:00",
> "13:00", "13:00")), .Names = c("start", "end"), row.names = c(NA,
> 20L), class = "data.frame")
>
> and I would like the data to look like this:
>
>>       t9   t10   t11   t12   t13   t14   t15   t16   t17
>> 1  FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
>> 2  FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
>> 3  FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
>> 4  FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
>> 5  FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
>> 6  FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
>> 7  FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
>> 8  FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
>> 9  FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
>> 10 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> Which means, that I just get a TRUE for every hour the event was 
> taking place. A finishing time of 16:00 means that t16 is FALSE, 
> because the event was finished until 16:00; 16:15 as end time would 
> result in t16 being TRUE.
> It would be nice if the function would add the variables needed (t9 
> ..) as well and depending on the times put in (no t9 if there is no 
> event starting before 10:00).
>
> Thanks for any suggestion,
> Stefan
>
> ______________________________________________
> 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