Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 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!


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

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...