henryjean
henryjean

Reputation: 102

Count number of points that are inside a pre-defined grid

I have a dataset that includes X,Y coordinates and I want to know how points in my dataset fall within each square in a grid that I've pre-defined.

Here is some sample code that generates the following plot.

What I'd like to be able to do is assign a Group ID to each observation that tells you which square that point falls in. That way I can then count the number of points within each Group ID

df <- data.frame(replicate(2,sample(0:100,100,rep=TRUE)))

  ggplot(df, aes(x = X1, y = X2)) + 
  geom_point()

enter image description here

Upvotes: 3

Views: 1002

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 146090

Use cut on the x and y coordinates with breaks every 25 or whatever, then you can use dplyr to count by group.

library(dplyr)
df %>% mutate(
  cut_x = cut(X1, breaks = seq(0, 100, by = 25), include.lowest = T),
  cut_y = cut(X2, breaks = seq(0, 100, by = 25), include.lowest = T)
) %>%
count(cut_x, cut_y)
# # A tibble: 16 x 3
#    cut_x    cut_y        n
#    <fct>    <fct>    <int>
#  1 [0,25]   [0,25]       6
#  2 [0,25]   (25,50]      4
#  3 [0,25]   (50,75]      6
#  4 [0,25]   (75,100]     9
#  5 (25,50]  [0,25]       3
#  6 (25,50]  (25,50]      2
#  7 (25,50]  (50,75]      6
#  8 (25,50]  (75,100]     6
#  9 (50,75]  [0,25]       5
# 10 (50,75]  (25,50]      6
# 11 (50,75]  (50,75]      6
# 12 (50,75]  (75,100]     4
# 13 (75,100] [0,25]       8
# 14 (75,100] (25,50]     13
# 15 (75,100] (50,75]     10
# 16 (75,100] (75,100]     6

Upvotes: 4

Related Questions