Мы можем использовать dplyr
пакет:
library(dplyr)
dat %>%
group_by(TIME = format(dat$TIME,format='%d/%m/%Y %H:00:00'), level1, level2) %>%
count(name = "Count")
#> # A tibble: 9 x 4
#> # Groups: TIME, level1, level2 [9]
#> TIME level1 level2 Count
#> <chr> <chr> <chr> <int>
#> 1 29/11/2019 00:00:00 A a 1
#> 2 29/11/2019 00:00:00 B a 1
#> 3 29/11/2019 00:00:00 B b 2
#> 4 29/11/2019 00:00:00 B c 1
#> 5 29/11/2019 01:00:00 A a 2
#> 6 29/11/2019 02:00:00 A a 1
#> 7 29/11/2019 02:00:00 B a 1
#> 8 29/11/2019 02:00:00 B b 2
#> 9 29/11/2019 02:00:00 B c 1
Данные: Это данные, которые я использовал. Пожалуйста, предоставьте свои данные, используя dput(dat)
вместо копирования / вставки.
structure(list(TIME = structure(c(1574985900, 1574985900, 1574986020,
1574986800, 1574985900, 1574990400, 1574990700, 1574992800, 1574992800,
1574992800, 1574994900, 1574995740), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), level1 = c("A", "B", "B", "B", "B", "A", "A",
"A", "B", "B", "B", "B"), level2 = c("a", "a", "b", "b", "c",
"a", "a", "a", "a", "b", "b", "c"), Occurrence = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), spec = structure(list(
cols = list(TIME = structure(list(format = "%d/%m/%Y %H:%M"), class = c("collector_datetime",
"collector")), level1 = structure(list(), class = c("collector_character",
"collector")), level2 = structure(list(), class = c("collector_character",
"collector")), Occurrence = structure(list(), class = c("collector_integer",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))