colin
colin

Reputation: 2666

Breaking a grid into 50 square subunits in R using x y coordinates

I have data with X-Y coordinates on a rectangular grid

dX <- runif(1000,0, 500)
dY <- runif( 500,0, 500)
d <- data.frame(dX, dY)

I want to assign 50 'plot numbers' to this data, such that anything between 0-100 dX and 0-100 dY is plot 1. anything between 100-200 dX and 1000-200 dY is plot 2. Etc. I can imagine writing 50 lines of code to do this, but I would really prefer to write 1!

Upvotes: 2

Views: 177

Answers (2)

d.b
d.b

Reputation: 32548

#DATA
set.seed(42)
dX <- runif(1000,0, 500)
dY <- runif( 500,0, 500)
d <- data.frame(dX, dY)

x_grid = seq(0, 500, 500/5)[-1]
y_grid = seq(0, 500, 500/5)[-1]
grids = expand.grid(x_grid, y_grid)
dX2 = ceiling(d$dX/100)*100
dY2 = ceiling(d$dY/100)*100
d$group = match(paste(dX2,dY2,sep = "-"), paste(grids$Var1,grids$Var2,sep="-"))
plot(d$dX, d$dY, type = "p", col = d$group, pch = 19)

enter image description here

Upvotes: 2

lmo
lmo

Reputation: 38500

Here is a solution using the base R functions cut and interaction.

d$gridGroup <- interaction(cut(d$dX, breaks=seq(0, 1000, by=100)),
                           cut(d$dY, breaks=seq(0, 1000, by=100)), sep="X")

head(d)
         dX       dY           gridGroup
1  56.85171 418.8170   (0,100]X(400,500]
2 311.14970 243.7331 (300,400]X(200,300]
3 304.63737  55.1685   (300,400]X(0,100]
4 311.68972 175.6815 (300,400]X(100,200]
5 430.45769 380.5315 (400,500]X(300,400]
6 320.15530 194.8324 (300,400]X(100,200]

This produces a factor variable with useful reference labels that correspond to the X grid and the Y grid. As suggested in the comments, cut can be used to produce a factor variable to depict intervals of a continuous variable. Then interaction can be used to produce the interaction of the two interval variables.

data

set.seed(1234)
dX <- runif(1000,0, 500)
dY <- runif( 500,0, 500)
d <- data.frame(dX, dY)

Upvotes: 4

Related Questions