I am creating something based on this useful post . I have three related questions for a dataset df:
machine ISOdatetime
1 M1 2013-08-21 18:16:39
2 M1 2013-08-21 18:20:44
3 M1 2013-08-21 18:21:42
4 M1 2013-08-21 18:46:09
5 M1 2013-08-21 18:46:27
6 M1 2013-08-21 19:01:13
etc
I want to find out how many values occur during one and a half hour periods and fit into a new data framework, for example:
machine ISOdatetime numberobs
1 M1 2013-08-21 18:30:00 3
2 M1 2013-08-21 19:00:00 2
3 M1 2013-08-21 19:30:00 1
etc
The following code, of course, works great for neat hourly lengths:
df2 <- data.frame(table(cut(df$ISOdatetime, breaks="hour")))
The following code is counted in 30-minute blocks, but does not start accurately with hourly / half-hour points (it takes the starting point from the first specified time, which is 18:16:39, and designates the beginning as 18:16: 00):
df2 <-data.frame(table(cut(df$ISOdatetime, breaks = "30 mins")))
Question 1. What could be an elegant solution? Should I indicate the required intervals with something like
ints <-c("18:00", "18:30", "19:00" ...)
, or is it not necessary?
2. , , df, "M2" df$machine, . . , subset "" , "". , "" ?
3. " " , , -, " " , , . , , , . breaks = hour, ?
, .
#
, .
unit nightof time date isodatetime time2
1 7849 2013-08-21 18:16:39 2013-08-21 2013-08-21 18:16:39 2013-08-22 04:00:00
2 7849 2013-08-21 18:20:44 2013-08-21 2013-08-21 18:20:44 2013-08-22 04:00:00
3 7849 2013-08-21 18:21:42 2013-08-21 2013-08-21 18:21:42 2013-08-22 04:00:00
etc
406 7849 2013-08-21 04:06:10 2013-08-22 2013-08-22 04:06:10 2013-08-22 14:00:00
407 7849 2013-08-21 04:06:12 2013-08-22 2013-08-22 04:06:12 2013-08-22 14:00:00
408 7849 2013-08-21 04:06:28 2013-08-22 2013-08-22 04:06:28 2013-08-22 14:00:00
str()
'data.frame': 408 obs. of 6 variables:
$ unit: int 7849 7849 7849 7849 7849 7849 7849 7849 7849 7849 ...
$ nightof: Date, format: "2013-08-21" "2013-08-21" "2013-08-21" "2013-08-21" ...
$ time: List of 408
..$ : chr "18:16:39"
..$ : chr "18:20:44"
.. [list output truncated]
$ date: Date, format: "2013-08-21" "2013-08-21" "2013-08-21" "2013-08-21" ...
$ isodatetime: POSIXlt, format: "2013-08-21 18:16:39" "2013-08-21 18:20:44" "2013-08-21 18:21:42" "2013-08-21 18:21:48" ...
$ time2: POSIXct, format: "2013-08-22 04:00:00" "2013-08-22 04:00:00" "2013-08-22 04:00:00" "2013-08-22 04:00:00" ...
, :
`mon$time2 <- with(mon, as.POSIXct(ceiling(as.numeric(isodatetime)/(30*60)) * (30*60), origin = "1970-01-01"))
with(mon, data.frame(table(time2)))
by(mon, mon$unit, function(x){data.frame(table(x$time2))})`
.
mon$unit: 7849
Var1 Freq
1 2013-08-22 04:00:00 27
2 2013-08-22 04:30:00 13
3 2013-08-22 05:00:00 16
4 2013-08-22 05:30:00 5
5 2013-08-22 06:00:00 8
6 2013-08-22 06:30:00 10
7 2013-08-22 07:00:00 25
8 2013-08-22 07:30:00 22
9 2013-08-22 08:00:00 61
10 2013-08-22 08:30:00 93
11 2013-08-22 09:00:00 54
12 2013-08-22 09:30:00 42
13 2013-08-22 10:00:00 11
14 2013-08-22 10:30:00 2
15 2013-08-22 11:00:00 2
16 2013-08-22 11:30:00 3
17 2013-08-22 12:00:00 2
18 2013-08-22 13:00:00 1
19 2013-08-22 14:00:00 11