Home > Enterprise >  How can I match coordinates with their associated values in R
How can I match coordinates with their associated values in R

Time:11-27

I have 250 points that I generated within a rectangle (-4,4)x(-6,6). If the popints are within a certain space they are blue and if they are outside of that space they are red.

enter image description here

The code I used for this is here, where i defined the confined space with squares:

library(sf)

border <- matrix(c(
-6, -4,
-6, 4,
 6, 4,
 6, -4,
-6, -4
 ), ncol  = 2, byrow = TRUE) |> 
 sfheaders::sfc_polygon() 

# sample random points
rand_points <- st_sample(border, size = 250)  

squares1 <- matrix(c(
 -4, 0,
 -4, 3,
 -1, 3,
 -1, 0,
 -4, -0
 ), ncol  = 2, byrow = TRUE) |> 
  sfheaders::sfc_polygon() 

 squares2 <- matrix(c(
  -2, -4,
  -2, -1,
  1, -1,
  1, -4,
  -2, -4
 ), ncol  = 2, byrow = TRUE) |> 
 sfheaders::sfc_polygon()

squares3 <- matrix(c(
 2, -2,
 2, 1,
 5, 1,
 5, -2,
 2, -2
 ), ncol  = 2, byrow = TRUE) |> 
  sfheaders::sfc_polygon()

 squares <- c(squares1, squares2, squares3)
       red_vals <- st_difference(rand_points, squares)
       blue_vals <- st_intersection(rand_points, squares)`
 plot(border)
 plot(negative_vals, add = TRUE, col = "red")
 plot(positive_vals, add = TRUE, col = "blue")

My goal is to match the points' coordinates with their expected value. Example:

enter image description here

In the table, the third coloumn is for the blue points and the fourth coloumn for the red. If the point at that coordinate is blue it gets a 1 and if it is not blue at that coordinate -1, and vice versa for the red points.

So far, I have attained the coordinates of all the points.

        y <- c(red_vals)
        x <- c(blue_vals)
        cdata <- c(x, y)
        coord <- st_coordinates(cdata)`

I am now stuck on trying to figure out how I can classify x and y to their respective coordinates and indicate this in a dataframe.

Any help is appreciated.

CodePudding user response:

You could do:

red_vals <- rand_points[rowSums(st_intersects(rand_points, squares, F)) == 0]
blue_vals <- st_intersection(rand_points, squares)

df <- rbind(cbind(st_coordinates(red_vals), PosGroup = 1, NegGroup = -1),
      cbind(st_coordinates(red_vals), PosGroup = -1, NegGroup = 1)) |>
      as.data.frame()

head(df)
#>             X           Y PosGroup NegGroup
#> X1 -5.2248158  0.03710509        1       -1
#> X2 -5.8932331 -1.41421992        1       -1
#> X3 -0.0609895  0.26541100        1       -1
#> X4  1.7345333 -3.04312404        1       -1
#> X5 -4.6801643  0.24656851        1       -1
#> X6  1.3190239  3.36491623        1       -1

Obviously the first few values are all red dots.

We can see that the points are correct by using this data frame to draw points in ggplot:

library(ggplot2)

df %>%
  ggplot()  
  geom_sf(data = squares)  
  geom_point(aes(X, Y, color = factor(PosGroup)), pch = 1, size = 3)  
  theme_classic()  
  scale_color_brewer(palette = "Set1", direction = -1)

enter image description here

  • Related