I have a dataframe df with three variable: city, state and country. I want to calculate 3 things
- Calculate the latitude/longitude for each row.
- Calculate the distance of each city from an arbitrary point, say country capital
- Calculate the elevation of each point from the lat/long.
I can use the dismo package for 1, but can’t figure out a way to “bulk process” from df, instead of copying and pasting the city, state and country names directly into the geocode(c()) code. As for 2 and 3, I am stumped completely. Any help would be appreciated.
EDIT NOTE: To other readers of this post… I appreciate the help from both Paul and Spacedman. The system won’t allow me to mark more than one response as correct. I gave Paul the thumbs up because he responded before Spaceman. Please read what both of them have taken the time to write up. Thanks.
I’ll share my thoughts on each of the points:
For 1. you can use
pasteto create an input vector for geocode:In regard to point number 2, after converting
dfto one the classes provided by thesppackage (SpatialPointsDataFrame, look at thecoordinatesfunction fromsp), you can use spDistsN1 to find the distance of all the points to one other point.The final is a bit more tricky, to find the height you need a DEM (digital elevation model). Maybe there is a more easy way along the lines of
geocodewhich I am not aware of.