I have a dataframe df
with a point each 0.1 unit:
df <- expand.grid(x = seq(0, 20, by = .1),
y = seq(0, 20, by = .1))
I defined a new dataframe grid
which has a point each 4 units:
grid <- expand.grid(xg = seq(0, 20, by = 4),
yg = seq(0, 20, by = 4))
I would like to use the points of grid
as nodes of a grid and determine the points in df
which fall inside its cells.
The information about the grid cell should be added to a new column in df
providing a string such as i.j
for each point, where i
and j
are the row and column index of the grid
cell, respectively. The new column should report NA
for df
points on the grid
lines.
For example, all df
points with 0 < x < 4 and 0 < y < 4 should be labeled as 1.1
, whereas points with 8 < x < 12 and 16 < y < 20 should be labeled as 3.5
and so on.
The ideal solution should be fine also for grids with different size, i.e. expand.grid(xg = seq(0, 20, by = 2), yg = seq(0, 20, by = 2)
.
Thanks for your help.