I am working with a data example from the GWmodel package. The dataset is called USelect.


USelect contains election outcome data at the county level from the 2004 US election as well as several census variables. Each county has an id number but hasn't been supplied with their county or even state names. The head of the data is below.


 winner unemploy pctcoled PEROVER65 pcturban WHITE
1   Bush      4.1     11.0      17.2      0.0  97.2
2   Bush     13.3     12.0      12.6      0.0  75.5
3   Bush      9.4     12.1      12.9     14.1  90.0
4   Bush     10.5     12.0      14.0     12.3  75.3
5   Bush     13.4     12.0      14.9      0.0  93.5
6   Bush      8.8     13.3      13.4      0.0  95.2

Is there is a method to retrieve and apply such names/labels to this file? I would like to have labels so that I can do effective interactive mapping with Leaflet.

I am aware of the Geocode package but unsure if can do what I'm looking for.

  • So do you mean (for the first row) "Can I find which county voted "Bush", had 4.1% unemployment, 11.0% college education, 17.2% over 65", etc etc? ie figure out the counties from the demographics? – Spacedman Oct 9 '17 at 12:02
  • Yes, I mean you could obviously do it manually e.g using a leftlet map to highlight each county and work it out but it would be very inefficient.. usually the area names are attached to the data so I'm not really sure why it's not the case here. – Henry Cann Oct 9 '17 at 13:10
> ?USelect2004

points to the source:

 Robinson, A. C. (2013). Geovisualization of the 2004 Presidential
 Election.  In: NATIONAL INSTITUTES OF HEALTH, P. S. U. (ed.). Penn
 State: <URL:

where you can download a zipped 2004 shapefile, extract and read:

> library(raster)
> us = shapefile("./2004_Election_Counties.shp")
> dim(us)
[1] 3111   69

this has the county names and loads more census data:

> names(us)
 [1] "NAME"       "STATE_NAME" "STATE_FIPS" "CNTY_FIPS"  "FIPS"      
 [6] "AREA"       "FIPS_num"   "Bush"       "Kerry"      "County_F"  
[11] "Nader"      "Total"      "Bush_pct"   "Kerry_pct"  "Nader_pct" 
[16] "MDratio"    "hosp"       "pcthisp"    "pcturban"   "urbrural"  
[21] "pctfemhh"   "pcincome"   "pctpoor"    "pctlt9ed"   "pcthsed"   

it doesn't have the winner, though, I think you have to compute that yourself from the Bush and Kerry (and Nader?) columns. Note the "Borderline" category:

 winner Categorical variable with three classes: i) Bush, ii) Kerry
      and iii) Borderline (supporting ratio for a candidate ranges
      from 0.45 to 0.55)

although if we can convince ourselves that the shapefile and USelect2004 are in the same order we can just copy the winner column across. Do the unemployment values agree?

> all(us$unemploy == USelect2004$unemploy)
[1] TRUE

yes, so I reckon you can do either:

> us$winner = USelect2004$winner

to stick the winner on the fuller data from the source, or:

> USelect2004$NAME = us$NAME

to stick the name on the simpler data set from the R package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.