1

This is a follow up to a previous question: How do I find the shortest distance from a point to a polygon edge on a map in R (point is outside polygon)

I have 80+ polygons in latlong coordinates outside a shoreline, and a shoreline with multiple points. I want to use a loop function to calculate the shortest distance between each point and each polygon. Currently, the polygons are named as follows: may.1, may.2, may.3 etc and the same for June, July, and August. First of all, R seems to re-order my files so that the order becomes: may.1, may.10, may.11 (you get the idea), and after may.19 i get may.2 and so on. I understand that R uses the first number as priority but I don't know how to fix this so that my dates remain in the correct order. I tried to create a list with the following command:

maplist <- list(may.1, may.2, may.3, ... aug.31)

and when I check "maplist" there is so much data that it will not fit in my console window and I have no idea how R has ordered my list or if it is even in the right format. I am thinking I should re-name my polygons but I'm not sure how so that the order remains chronological by month. I don't know if this is relevant but I have many missing dates from the 4-month dataset so I don;t know if that affects the numbering of my list.

My ultimate goal is to use the loop function

for(i in 1:length(maplist))
{
print(maplist[[i]])
}

to get an output where the first column has all my polygons and the subsequent columns have the shortest distance from each polygon to my points on the shoreline (which are also in latlong coordinates of course). So the row names should be my polygon names, and column names should be my point coordinates. I want to use

dist2Line(c(latitude, longitude), <polygon.name>, distfun=distHaversine)

to calculate the distance.

My main question is how to name and create my list of polygons so that I can accomplish what I want to do. The rest is just extra information to make my objective more clear and so that you know what functions I am planning on using in case that affects how I need to make my list.. Thanks again for any help you can provide!

Addition: I tried to test a loop of two polygons and two points and I get an error message saying Error: Attempt to apply non-function. I used:

for(i in 1:2(maplist))
{
print(maplist[[i]])
}

where 1 and 2 are map coordinates defined as i. Sorry for all the questions, I have read up on loop functions online and in R books but I am really struggling to find appropriate guidelines for my data.

Community
  • 1
  • 1
user3281487
  • 97
  • 1
  • 1
  • 9

1 Answers1

0

If you want an equivalent to:

for(i in 1:length(maplist))
{
print(maplist[[i]])
}

Then why not:

 lapply(maplist, print)
IRTFM
  • 258,963
  • 21
  • 364
  • 487