2015-08-25 10 views
6

Ich möchte ein SpatialPolygonsDataFrame fusionieren:Merge Datenrahmen mit SpatialPolygonsDataFrame

# From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html 
states <- readOGR(dsn = "./cb_2014_us_state_20m.shp", 
        layer = "cb_2014_us_state_20m", verbose = FALSE) 

mit einem normalen Datenrahmen:

my_counts <- data.frame(
    State = c(
    "CA", "TX", "IL", "FL", "NY", "OH", 
    "NJ", "GA", "MI", "PA", "MA", "CO", "AZ", "NC", "VA", "WA", "IN", 
    "MD", "MN", "WI", "MO", "TN", "IA", "KY", "LA", "SC", "CT", "AL", 
    "KS", "OR", "OK", "AR", "NV", "UT", "NE", "ID", "MS", "DC", "NM", 
    "NH", "ME", "AK", "RI", "MT", "HI", "WV", "SD", "ND", "DE", "VT", 
    "WY", "PR", "GU", "VI", "MP", "AS", "na", "MH", "FM", "PW" 
), 
    count = c(
    1590533L, 1016328L, 754535L, 742603L, 714205L, 
    538719L, 477278L, 452064L, 437162L, 428616L, 420332L, 391084L, 
    380853L, 354601L, 342533L, 335505L, 294670L, 286026L, 273427L, 
    246172L, 238968L, 236037L, 235030L, 209514L, 199013L, 191707L, 
    185521L, 179931L, 163477L, 159862L, 142610L, 136006L, 120111L, 
    117338L, 112671L, 106176L, 102564L, 100168L, 97496L, 69881L, 
    69508L, 68684L, 65631L, 62109L, 61123L, 57300L, 57254L, 56091L, 
    51696L, 33944L, 32136L, 4822L, 598L, 468L, 49L, 19L, 17L, 
    11L, 2L, 1L 
) 
) 

Das Ziel ist, das Ergebnis zu verwenden, um eine Karte mit leaflet

zu machen

Ich versuchte sp :: merge

df1 <- sp::merge(x= states, y=my_counts) 

aber ich erhalte eine Fehlermeldung:

Error in table(y[, by.y]) : attempt to set an attribute on NULL 
+0

Noch ein Tipp (da @bondeddust die Antwort genagelt hat) soll 'stringsAsFactors = FALSE' im' readOGR' Aufruf _and_ in der 'data.frame' Erstellung verwendet werden, um mögliche Faktor/Zeichen Probleme zu vermeiden, während Sie die Daten manipulieren. – hrbrmstr

Antwort

9

Caveat: Ich habe das nie getan, bevor so bin ich „meine Art und Weise das Gefühl um“. Zuerst das Objekt betrachten - states:

Hinweis: Dies war mit RGDAL_0.9-3 und SP_1.1-1 geladen unter R 3.2.1 (und mit GDAL auf meinem OSX-System installiert, von Kingchaos, IIRC):

> str(states) 
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots 
    [email protected] data  :'data.frame': 52 obs. of 9 variables: 
    .. ..$ STATEFP : Factor w/ 52 levels "01","02","04",..: 5 9 10 11 13 14 16 18 19 21 ... 
    .. ..$ STATENS : Factor w/ 52 levels "00068085","00294478",..: 22 17 2 18 27 28 29 30 16 19 ... 
    .. ..$ AFFGEOID: Factor w/ 52 levels "0400000US01",..: 5 9 10 11 13 14 16 18 19 21 ... 
    .. ..$ GEOID : Factor w/ 52 levels "01","02","04",..: 5 9 10 11 13 14 16 18 19 21 ... 
    .. ..$ STUSPS : Factor w/ 52 levels "AK","AL","AR",..: 5 8 10 11 14 15 13 18 19 21 ... 
    .. ..$ NAME : Factor w/ 52 levels "Alabama","Alaska",..: 5 9 10 11 13 14 16 18 19 21 ... 
    .. ..$ LSAD : Factor w/ 1 level "00": 1 1 1 1 1 1 1 1 1 1 ... 
    .. ..$ ALAND : num [1:52] 4.03e+11 1.58e+08 1.39e+11 1.49e+11 2.14e+11 ... 
    .. ..$ AWATER : num [1:52] 2.05e+10 1.86e+07 3.14e+10 4.95e+09 2.40e+09 ... 
    [email protected] polygons :List of 52 
    .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots 
    .. .. .. [email protected] Polygons :List of 6 
    .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots 
    .. .. .. .. .. .. [email protected] labpt : num [1:2] -118.4 33.4 
    .. .. .. .. .. .. [email protected] area : num 0.0259 
    .. .. .. .. .. .. [email protected] hole : logi FALSE 
##### Snipped rest of output ............................ 

So nach Hilfe zur Zusammenführung und Lesen suchen:

?merge # and choosing the option for: 

Merge a Spatial* object having attributes with a data.frame 
(in package sp in library /Library/Frameworks/R.framework/Versions/3.2/Resources/library) 

ich beschlossen, zu versuchen (und scheinen erfolgreich gewesen zu sein:

> newobj <- merge(states, my_counts, by.x="STUSPS", by.y="State") 
Warning message: 
In .local(x, y, ...) : 8 records in y cannot be matched to x 

> names([email protected]) 
[1] "STUSPS" "STATEFP" "STATENS" "AFFGEOID" "GEOID" "NAME"  
[7] "LSAD"  "ALAND" "AWATER" "count" 

Die Warnung ist sinnvoll. Sie scheinen einige zusätzliche „Staaten“ nicht von den Autoren, dass zu erwarten haben „so“ SHP-Datei:

> length(table(my_counts$State)) 
[1] 60 
> length(unique([email protected]$STUSPS)) 
[1] 52 

Die moralische

Sie an den names -Werten in den beiden Objekten suchen sollten, wenn Sie fusionieren:

> names(states) 
[1] "STATEFP" "STATENS" "AFFGEOID" "GEOID" "STUSPS" "NAME"  "LSAD"  
[8] "ALAND" "AWATER" 

> names(my_counts) 
[1] "State" "count" 
+0

Sie können auch direkt mit dem '@ data'-Slot arbeiten (nicht empfohlen, es sei denn, man weiß, was sie tun) und der eigentliche Schlüssel für diese Prozedur ist, die Reihenfolge der Zeilen ODER der Rownames nicht zu stören. – hrbrmstr

0

vielleicht sollten Sie das Argument "unvergleichlich", wie im Beispiel hinzufügen:

"merge(x, y, by=intersect(names(x), names(y)), 

by.x = nach, by.y = nach, all.x = WAHR, Suffixe = c (". X", ". Y"), unvergleichbar = NULL, ...) "

+0

Sie vermissen den wesentlichen Punkt: dass der Standardwert für "by" NICHT erfolgreich ist. –

Verwandte Themen