Is there more efficient and faster method in R for finding point in multipolygon?

73 Views Asked by At

I use below code in RStudio to run through around 6500 rows in sf_data1. Sf_data2 has 19 rows. Here are pictures of sf_data1 and sf_data2:

sf_data1

sf_data2

example of sf_data2 multipolygon "Konversiokerroin" is the variable1 below in the code.

The geometry I have in sf_data1 is point(x,y) and in sf_data2 it's multipolygon. And if the intersect -logical step proves to be true, I will take the value from sf_data2$variable[j] and put it in sf_data1$variable[i].

It doesn't take too long to run through all the entries, but do you think it could be faster than my current 15 ish minutes.

The code:

sf_data1$variable1 <- c(0)

for (i in 1:nrow(sf_data1)) {
  for (j in 1:nrow(sf_data2)) {
  ifelse(st_intersects(sf_data1$geometry[i], sf_data2$geometry[j]),
         sf_data1$variable1[i] <- sf_data2$variable1[j],"")
  }
}

I have tried to limit different uses of funtions and calls, but this is the result I found was fastest for me. I expected the code to run in maybe 5 min ish, not 15 min ish.

0

There are 0 best solutions below