Given a grid object (Grid1d, Grid2d or Grid3d) and a matrix or dataframe of points, it returns the index of each point in the grid. For a 1d grid it is counted from min to max; for a 2d grid it is counted from xmin, ymin to xmax, ymax increasing x or y faster according to the "by" parameter of the grid; for a 3d grid it is counted from xmin, ymin, zmin to xmax, ymax, zmax increasing x or z faster according to the "by" parameter of the grid.
Value
A vector in which the i-th element represents the index of the grid cell to which the i-th point belongs
Examples
# 1. Generate random points on a plane
df_points <- data.frame(
x = c(rnorm(n = 50000, mean = -2), rnorm(n = 50000, mean = 2)),
y = c(rnorm(n = 50000, mean = 1), rnorm(n = 50000, mean = -1))
)
# 2. Define a grid that contains all the points generated
the_grid <- makeGrid2d(
xmin = floor(min(df_points$x)), ymin = floor(min(df_points$y)),
xmax = ceiling(max(df_points$x)), ymax = ceiling(max(df_points$y)),
xcell = 50, ycell = 50, by = "v"
)
# 3. Match each point with a grid element
grid_index <- getCell(the_grid, df_points)
df_points$grid_index <- grid_index
head(df_points)
#> x y grid_index
#> 1 -1.5336231 1.743683 1083
#> 2 -2.2422001 1.502113 932
#> 3 -0.9742401 1.824995 1183
#> 4 -1.6445622 2.726510 1037
#> 5 -1.6702952 0.631321 1028
#> 6 -1.3757165 0.945526 1079