Counting Time Intervals

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
+4
3

lubridate , .. . , , . . "" , "toke place", . , , , .

require(lubridate)
set.seed(1234)
example.dat <- data.frame(
  machine = paste("M", sample(1:2, 100, replace = T), sep = ""),
  dates = sort(as.POSIXct(sample(1377100000:1377110000, 100), origin = "1960-01-01"))
  )
example.dat <- example.dat[order(example.dat$machine), ]

halfHours <- ifelse(minute(example.dat$dates) < 30, 0, 30)
example.dat$datesHH <- example.dat$dates
minute(example.dat$datesHH) <- halfHours; second(example.dat$datesHH) <- 0

data.frame(table(example.dat[ ,c(1,3)]))
+3

30 , ceiling:

df$time <- with(df, as.POSIXct(ceiling(as.numeric(ISOdatetime)/(30*60)) * (30*60),
                                origin = "1970-01-01"))

# counts for each combination of time and machine
with(df, data.frame(table(time, machine)))
+1

, . :

crashDate <- as.Date(c("1908-09-17","1912-07-12","1913-08-06",
                        "1913-09-09","1913-10-17"))
df <- data.frame(date=crashDate)
byYears <- DAAGviz::eventCounts(data=df, dateCol="date",
                                 from=as.Date("1908-01-01"),
                                 by="1 year")

:

> byYears
            Date n_
    1 1908-01-01  1
    2 1909-01-01  0
    3 1910-01-01  0
    4 1911-01-01  0
    5 1912-01-01  1
    6 1913-01-01  3

categoryCol () , , ( ). takeOnly , data, , , . by : "1 day" "1 week", "4 weeks", "1 month", "1 quarter", "1 year", "10 years". help(seq.Date).

, , , , .

eventCounts <-
function (data, dateCol = "Date", from = NULL, to = NULL, by = "1 month", 
          categoryCol = NULL, takeOnly = NULL, prefix = "n_") 
{
    checkCols <- c(dateCol, categoryCol) %in% names(data)
    if (!is.null(categoryCol) & !all(checkCols)) {
        txt <- paste("Name(s)", c(dateCol, categoryCol)[!checkCols], 
                     "not found in", deparse(data))
        stop(txt)
    }
    if (!is.null(takeOnly)) {
        subdat <- eval(parse(text = takeOnly), data)
        data <- subset(data, subdat)
    }
    date <- data[, dateCol]
    if (!is(date, "Date")) {
        date <- try(as.Date(date), silent = TRUE)
        if (class(date) == "try-error") 
            stop(paste("Column", dateCol, "must hold a date object"))
    }
    if (is.null(from)) 
        from <- min(date)
    if (is.null(to)) 
        to <- max(date)
    dateBreaks <- seq(from = from, to = to, by = by)
    dateBreaks <- c(dateBreaks, max(dateBreaks) + diff(dateBreaks[1:2]))
    countDF <- data.frame(Date = dateBreaks[-length(dateBreaks)])
    if (!is.null(categoryCol)) 
        categs <- names(table(data[, categoryCol]))
    else categs <- ""
    for (cat in categs) {
        if (!is.null(categoryCol)) 
            select <- data[, categoryCol] == cat
        else select <- rep(TRUE, nrow(countDF))
        cutDates <- cut(date[select], dateBreaks, right = FALSE)
        countNam <- paste0(prefix, gsub(" ", "", cat))
        countDF[, countNam] <- as.vector(table(cutDates))
    }
    countDF
}
0

Source: https://habr.com/ru/post/1523859/


All Articles