Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

coordinates - Assign name based on points in polygons from 2 dataframes in R

i'm stuck and frustrated.

I have two data frames:

A: one large dataset (4900000+ rows) with temperatures, latitude and longitude, date.

temp <- c(sst, lat, lon, date)

B: one with 209 polygon names and coordinate boundaries (North, South, East and West).

icesrect <- c(AreaCode, SOUTH,EAST, NORTH, WEST)

For each row in A I need to find which polygon(B) that A's coordinates fits within, and then assign said polygon name to the row in A. The final product should look likes this c(sst, lat, lon,date AreaCode).

I know it can be done with the in.polygon function, but the 209 polygons makes it extremely tedious. I figure that a for loop is the way to go, and i have tried this:

for (i in c(1:nrow(temp))){
indes <- which(((temp$lat[i] > icesrect$SOUTH) & (temp$lat[i] < icesrect$NORTH) & (temp$lon[i] > icesrect$WEST) & (temp$lon[i] < icesrect$EAST )))
temp$AreaCode[i] <- icesrect$AreaCode[indes]     }

Unfortunately this doesn't work.

I hope someone can help!

question from:https://stackoverflow.com/questions/65600288/assign-name-based-on-points-in-polygons-from-2-dataframes-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...