[R] Creating categories from a date-time object
jim holtman
jholtman at gmail.com
Wed Feb 15 00:56:58 CET 2012
Will this do it for you:
> tmp <- seq(as.POSIXct('2011-08-01 13:00'), as.POSIXct('2011-09-02 03:00'), by='45 min')
> # get just the hours
> hours <- format(tmp, "%H")
> weekdays <- format(tmp, "%w") %in% c('1', '2', '3', '4','5')
> office <- (hours >= '08') & (hours < '18') & weekdays
> result <- data.frame(time = tmp, office = office)
> head(result, 40)
time office
1 2011-08-01 13:00:00 TRUE
2 2011-08-01 13:45:00 TRUE
3 2011-08-01 14:30:00 TRUE
4 2011-08-01 15:15:00 TRUE
5 2011-08-01 16:00:00 TRUE
6 2011-08-01 16:45:00 TRUE
7 2011-08-01 17:30:00 TRUE
8 2011-08-01 18:15:00 FALSE
9 2011-08-01 19:00:00 FALSE
10 2011-08-01 19:45:00 FALSE
11 2011-08-01 20:30:00 FALSE
12 2011-08-01 21:15:00 FALSE
13 2011-08-01 22:00:00 FALSE
14 2011-08-01 22:45:00 FALSE
15 2011-08-01 23:30:00 FALSE
16 2011-08-02 00:15:00 FALSE
17 2011-08-02 01:00:00 FALSE
18 2011-08-02 01:45:00 FALSE
19 2011-08-02 02:30:00 FALSE
20 2011-08-02 03:15:00 FALSE
21 2011-08-02 04:00:00 FALSE
22 2011-08-02 04:45:00 FALSE
23 2011-08-02 05:30:00 FALSE
24 2011-08-02 06:15:00 FALSE
25 2011-08-02 07:00:00 FALSE
26 2011-08-02 07:45:00 FALSE
27 2011-08-02 08:30:00 TRUE
28 2011-08-02 09:15:00 TRUE
29 2011-08-02 10:00:00 TRUE
30 2011-08-02 10:45:00 TRUE
31 2011-08-02 11:30:00 TRUE
32 2011-08-02 12:15:00 TRUE
33 2011-08-02 13:00:00 TRUE
34 2011-08-02 13:45:00 TRUE
35 2011-08-02 14:30:00 TRUE
36 2011-08-02 15:15:00 TRUE
37 2011-08-02 16:00:00 TRUE
38 2011-08-02 16:45:00 TRUE
39 2011-08-02 17:30:00 TRUE
40 2011-08-02 18:15:00 FALSE
> View(result)
On Tue, Feb 14, 2012 at 2:35 PM, Jose Bustos Melo <jbustosmelo at yahoo.es> wrote:
> Hello R-List,
>
> I have a question about recoding from a date time object. I have tried using as.POSIXct objects and Chron Objects, but I can get the what I want.
>
> I need to create a new variable from a date-time object, adding "Office Time" for those events that happens between 08:00:00 to 18:00:00 and "Out of Office" all the others, but not including weekends. I have created a fake data.
>
> tmp <- seq(as.POSIXct('2011-08-01 13:00'), as.POSIXct('2011-09-02 03:00'), by='45 min')
>
> Is there any valid way to do it? I have spend so much time without any good luck and I don't have any good code to show!
> Thanks in advance!
> José
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list