I have a data frame with m rows and n columns, all values normalized to be between 0 and 1.
I would like each point to define an n-dimensional cube (preferably the point would be at the center of this cube, depending on whether the value in each axis is central or not) with each side equal to 0.2, and count how many data points lie in this cube.
For example:
df <- structure(list(x1 = c(0, 0.01, 0.05, 0.07, 0.1, 0.11, 0.16, 0.18,
0.2, 0.25, 0.5), x2 = c(0.05, 0.3, 0.1, 0.17, 0.38, 0.01, 0.04,
0.05, 0.11, 0.21, 0.26), x3 = c(0.4, 0.07, 0.09, 0.1, 0.23, 0.4,
0.2, 0.11, 0.01, 0.34, 0.22)), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"))
The first point cannot be a center of a cube, because its x and y values are too close to 0. The cube it defines is given by the constraints:
x1 >= 0 and x1 <= 0.2
x2 >= 0 and x2 <= 0.2
x3 >= 0.3 and x3 <= 0.5
So the first cube contains only the points (0, 0.05, 0.4) and (0.11, 0.01, 0.4).
The second point defines the cube:
x1 >= 0 and x1 <= 0.2
x2 >= 0.2 and x2 <= 0.4
x3 >= 0 and x3 <= 0.2
and contains only itself.
Now, I would like to be able to do this filtering efficiently for arbitrary n and m (base or dplyr please).
Any ideas?