3

How can I adapt the code given in this answer for county?

Latitude Longitude Coordinates to State Code in R

Please help. Thanks.

Community
  • 1
  • 1
Geekuna Matata
  • 1,349
  • 5
  • 19
  • 38
  • 6
    Simply change `map("state"` to `map("county"` in line 12 of the code block in my answer to that question. I tested with the coordinates given at the end of that code block, and it correctly returned these two counties: `"wisconsin,juneau" "oregon,crook"`. – Josh O'Brien Apr 14 '14 at 19:46
  • Thanks. That works! Just an aside, I am getting, "rhode island,kent". How can I get only Kent? That is only the county name. – Geekuna Matata Apr 14 '14 at 19:57
  • 2
    Glad that worked. Once you have the names (here contained in `x <- c("rhode island,kent", "oregon,crook")`), do something like `sapply(strsplit(x, ","), function(X) X[2])` – Josh O'Brien Apr 14 '14 at 20:01
  • Thank you. Sorry for asking too many questions. But it just occurred to me that I may not able to use it this way as there are lots of counties with the same name (I have to match with another data). Is there a way to modify your code to convert Lat-Long to FIPS code? Thanks a ton! – Geekuna Matata Apr 14 '14 at 20:04
  • As far as I know, the **maps** package doesn't include any FIPS code info, so you'll either need to import some spatial data that does associate FIPS codes with polygons, or create your own dictionary that associates FIPS codes with each of the county names returned **maps** package. – Josh O'Brien Apr 14 '14 at 20:12
  • Thanks. Let me put that as a separate question. Thank you very much. – Geekuna Matata Apr 14 '14 at 20:13
  • 2
    Actually, I just checked and it looks like you're in luck: the **maps** package ships with a data.frame named `county.fips` which associates a FIPS code with each polygon name. Do `data(county.fips)` to load it, and then `with(county.fips, fips[match(x, polyname)])` to get the FIPS codes from the character vector of county names returned by **maps**. – Josh O'Brien Apr 14 '14 at 20:16
  • Thanks! I loaded it. So now do I make some changes in your code? Or simply match the output from your code with this database? – Geekuna Matata Apr 14 '14 at 20:18
  • I added an example while you were typing your last question ;) As you'll see, the answer to your question is the latter: first get the county names, and then use `county.fips` to lookup the corresponding FIPS codes. – Josh O'Brien Apr 14 '14 at 20:18
  • Thank you. I am getting this though: Error in match(x, polyname) : object 'x' not found ..............I maybe putting it in the wrong place or doing something silly. :) – Geekuna Matata Apr 14 '14 at 20:25
  • Have you made sure that your `x` (or whatever you named the output of my county-finding routine) is a character vector, like `x <- c("rhode island,kent", "oregon,crook")`? I won't be able to help you any more, but figure you should be able to figure this out on your own. Cheers. – Josh O'Brien Apr 14 '14 at 20:27
  • Figured it out. Thanks! :) – Geekuna Matata Apr 14 '14 at 21:07
  • Unfortunately, I get lots of NA values for FIPS though the system works. Any idea what the problem could be? – Geekuna Matata Apr 14 '14 at 21:50
  • Nope! Maybe some of your points don't fall within any counties?? (i.e. do you have NA values a bit farther upstream, in the county character strings themselves?) – Josh O'Brien Apr 14 '14 at 21:53
  • Basically, I am trying to match YOUR fips codes with this set of codes in the following text file. The goal is to read the class eventually. http://koeppen-geiger.vu-wien.ac.at/data/KoeppenGeiger.UScounty.txt For example, the code 23017 is a legitimate county Maine. But your modified code doesn't output 23017. – Geekuna Matata Apr 14 '14 at 22:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50717/discussion-between-geekuna-matata-and-josh-obrien) – Geekuna Matata Apr 15 '14 at 18:37

2 Answers2

-2

it's well documented. for that kind of job you'll definitely have to use an external data source, no matter which one

anyway, it's an XML file you retrieve and as i could see R has very good XML support

-3

http://developer.here.com/ is an awesome API. it provides you with plenty of info based on coordinates

basically it's a REST API