Все, что вы просите, - это какое-то многомерное объединение.
Я сгенерировал случайный набор данных dt
отправления и назначения с целью демонстрации. Выходной результат - data.table
, который дает следующую информацию о наиболее частой траектории:
- Нижний и верхний пределы координат xy, которые определяют источник сетка
- Нижний и верхний пределы координат xy, которые определяют пункт назначения сетка
- Количество
library(data.table)
library(magrittr)
N <- 5000
set.seed(123)
gp <- 0.1 #grid precision
# Generate an example dataset -----
{
dt <- data.table(
origin_x = rnorm(N, 1, 0.1),
origin_y = rnorm(N, 2, 0.1),
destination_x = rnorm(N, 11, 0.1),
destination_y = rnorm(N, 12, 0.1)
)
}
# Grid formation ----
{
## Defining the ranges (LL and UL stand for lower and upper limits, respectively) ----
{
origin_x_LL <- dt[, origin_x] %>% min %>% divide_by(gp) %>% floor %>% multiply_by(gp)
origin_x_UL <- dt[, origin_x] %>% max %>% divide_by(gp) %>% ceiling %>% multiply_by(gp)
origin_y_LL <- dt[, origin_y] %>% min %>% divide_by(gp) %>% floor %>% multiply_by(gp)
origin_y_UL <- dt[, origin_y] %>% max %>% divide_by(gp) %>% ceiling %>% multiply_by(gp)
destination_x_LL <- dt[, destination_x] %>% min %>% divide_by(gp) %>% floor %>% multiply_by(gp)
destination_x_UL <- dt[, destination_x] %>% max %>% divide_by(gp) %>% ceiling %>% multiply_by(gp)
destination_y_LL <- dt[, destination_y] %>% min %>% divide_by(gp) %>% floor %>% multiply_by(gp)
destination_y_UL <- dt[, destination_y] %>% max %>% divide_by(gp) %>% ceiling %>% multiply_by(gp)
}
## Forming the breaks for binning ----
{
origin_x_brks <- seq(origin_x_LL, origin_x_UL, by = gp)
origin_y_brks <- seq(origin_y_LL, origin_y_UL, by = gp)
destination_x_brks <- seq(destination_x_LL, destination_x_UL, by = gp)
destination_y_brks <- seq(destination_y_LL, destination_y_UL, by = gp)
}
## Computing the number of bins ----
{
origin_x_Nbin <- length(origin_x_brks) - 1L
origin_y_Nbin <- length(origin_y_brks) - 1L
destination_x_Nbin <- length(destination_x_brks) - 1L
destination_y_Nbin <- length(destination_y_brks) - 1L
}
## Binning ----
{
origin_x_bin <- .bincode(dt[, origin_x], origin_x_brks, include.lowest = T)
origin_y_bin <- .bincode(dt[, origin_y], origin_y_brks, include.lowest = T)
destination_x_bin <- .bincode(dt[, destination_x], destination_x_brks, include.lowest = T)
destination_y_bin <- .bincode(dt[, destination_y], destination_y_brks, include.lowest = T)
}
}
# Counting grid frequency ----
{
grid_count <-
lapply(seq(origin_x_Nbin), function(i) {
lapply(seq(origin_y_Nbin), function(j) {
lapply(seq(destination_x_Nbin), function(m) {
lapply(seq(destination_y_Nbin), function(n) {
this_count = which(origin_x_bin == i & origin_y_bin == j & destination_x_bin == m & destination_y_bin == n) %>% length
return(data.table(origin_x_LL = origin_x_brks[i], origin_x_UL = origin_x_brks[i + 1],
origin_y_LL = origin_y_brks[j], origin_y_UL = origin_y_brks[j + 1],
destination_x_LL = destination_x_brks[m], destination_x_UL = destination_x_brks[m + 1],
destination_y_LL = destination_y_brks[n], destination_y_UL = destination_y_brks[n + 1],
count = this_count))
}) %>% rbindlist
}) %>% rbindlist
}) %>% rbindlist
}) %>% rbindlist
}
# Getting the most frequent grid ----
{
print(grid_count[count == max(count)])
}