0

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.

JKling
  • 1
  • 1
  • Whats the data looking like? An example would help – Just James Apr 05 '23 at 20:18
  • I'll add an example in the description. – JKling Apr 05 '23 at 20:22
  • 2
    If you can, format it within your post here. A pic is hard to work with – Just James Apr 05 '23 at 20:40
  • Does this answer your question? If not, you could use the example data from here to create an example of your issue. https://stackoverflow.com/q/72234302/12400385 – nniloc Apr 05 '23 at 20:55
  • I see now that this challenge is not easily tested without the example data. I have to look into how I could provide it here more clearly. I can do that tomorrow. I'm new here so, I try to learn to provide more in depth description of my situation with example data to work with. /Jouni – JKling Apr 05 '23 at 21:13
  • To be a little more clear, I think you need some kind of join rather than a for loop. Best guess is you need `st_join(sf_data1, sf_data2)` – nniloc Apr 05 '23 at 21:20

0 Answers0