[R-sig-Geo] [Help] Error in spChFIDs(SP, x) : lengths differ
Ignacio Martinez
ignacio82 at gmail.com
Fri Sep 4 17:59:17 CEST 2015
I'm not sure what I'm missing. This is not working (same error):
counts <-
structure(
list(
STUSPS = c(
"CA", "NC", "TX", "FL", "VA", "OH",
"NY", "GA", "IL", "WA", "CO", "AZ", "MD", "LA", "SC", "KS",
"DC",
"TN", "MA", "MI", "MN", "NJ", "WI", "PA", "AL", "KY", "OK",
"MO",
"ID", "MS", "NM", "IN", "NV", "AR", "OR", "AK", "UT", "IA",
"MT",
"HI", "NE", "CT", "WV", "WY", "SD", "VT", "ND", "ME", "RI",
"NH",
"DE", "PR", "GU", "VI", "MP", "AS"
), count = c(
36511L, 27513L,
25861L, 21099L, 19415L, 17012L, 15860L, 14362L, 13923L, 13753L,
11671L, 10540L, 9777L, 8923L, 8355L, 8219L, 8142L, 8076L, 7985L,
7770L, 7662L, 7531L, 7273L, 7212L, 7042L, 6708L, 6674L, 6429L,
6239L, 5580L, 5395L, 5172L, 5013L, 4933L, 4911L, 4797L, 4694L,
4458L, 3873L, 3757L, 3608L, 3111L, 2604L, 2217L, 2156L, 2143L,
2030L, 1544L, 1417L, 1168L, 772L, 531L, 148L, 63L, 7L, 2L
)
), .Names = c("STUSPS",
"count"), class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-56L)
)
x = c("leaflet", "rgdal", "maptools", "mapproj", "rgeos", "dplyr", "sp")
lapply(x, library, character.only = TRUE)
# From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
states <- readOGR(dsn = "./cb_2014_us_state_5m.shp",
layer = "cb_2014_us_state_5m", verbose = FALSE)
# Make a copy of the SPDF attribute table, and then work normally, as
with any data.frame/data.table object
states.df <- states at data
# Create an explicit attribute to keep polygons IDs (useful to
"re-attach" the table to the polygons later)
states.df <- states.df %>% mutate(rn=row.names(states))
# join them
US = merge(states.df, counts) # uses sp::merge.Spatial
US = US[-which(US$STUSPS == "PR"),]
# Re-attach the attribute table to the SPDF
states at data <- US
# Make sure polygons IDs and data.frame row.names match
states <- spChFIDs(states, states$rn)
Thanks!
On Fri, Sep 4, 2015 at 11:47 AM Edzer Pebesma <edzer.pebesma at uni-muenster.de>
wrote:
> On 09/04/2015 03:53 PM, Ignacio Martinez wrote:
> > Thanks Edzer. I think I understand the problem better now (this is all
> > very new to me). Is there a solution so I can generate the map with my
> data?
> >
>
> The STUSPS fields of counts and states match, but you seem to filter the
> attribute table after you merged counts and states, which drops PR, but
> doesn't take it out of states:
>
> > match(c("PR", "na", "MH", "FM", "PW"), counts$STUSPS)
> [1] 52 NA NA NA NA
>
> So, in case you want to deselect PR, a shorter way to get there is
>
> > m = merge(states, counts) # uses sp::merge.Spatial
> > m = m[-which(m$STUSPS == "PR"),]
> > length(m)
> [1] 55
>
>
>
> > Thanks again!
> >
> > On Fri, Sep 4, 2015 at 9:51 AM Edzer Pebesma
> > <edzer.pebesma at uni-muenster.de <mailto:edzer.pebesma at uni-muenster.de>>
> > wrote:
> >
> >
> >
> > On 09/04/2015 03:29 PM, Ignacio Martinez wrote:
> > > I'm trying to create a map using leaflet. I'm basically following
> this
> > > <https://github.com/rstudio/leaflet/issues/169>but my data is a
> bit
> > > different this time around. Additionally, I'm using dplyr instead
> of
> > > data.table.
> > >
> > > This is the code i'm trying to run:
> > >
> > > counts <-
> > > structure(
> > > list(
> > > STUSPS = c(
> > > "CA", "NC", "TX", "FL", "VA", "OH",
> > > "NY", "GA", "IL", "WA", "CO", "AZ", "MD", "LA", "SC",
> > "KS",
> > > "DC",
> > > "TN", "MA", "MI", "MN", "NJ", "WI", "PA", "AL", "KY",
> > "OK",
> > > "MO",
> > > "ID", "MS", "NM", "IN", "NV", "AR", "OR", "AK", "UT",
> > "IA",
> > > "MT",
> > > "HI", "NE", "CT", "WV", "WY", "SD", "VT", "ND", "ME",
> > "RI",
> > > "NH",
> > > "DE", "PR", "GU", "VI", "MP", "AS"
> > > ), count = c(
> > > 36511L, 27513L,
> > > 25861L, 21099L, 19415L, 17012L, 15860L, 14362L,
> > 13923L, 13753L,
> > > 11671L, 10540L, 9777L, 8923L, 8355L, 8219L, 8142L,
> > 8076L, 7985L,
> > > 7770L, 7662L, 7531L, 7273L, 7212L, 7042L, 6708L,
> > 6674L, 6429L,
> > > 6239L, 5580L, 5395L, 5172L, 5013L, 4933L, 4911L,
> > 4797L, 4694L,
> > > 4458L, 3873L, 3757L, 3608L, 3111L, 2604L, 2217L,
> > 2156L, 2143L,
> > > 2030L, 1544L, 1417L, 1168L, 772L, 531L, 148L, 63L, 7L,
> 2L
> > > )
> > > ), .Names = c("STUSPS",
> > > "count"), class = c("tbl_df", "tbl",
> > "data.frame"),
> > > row.names = c(NA,-56L)
> > > )
> > >
> > >
> > > x = c("leaflet", "rgdal", "maptools", "mapproj", "rgeos",
> "dplyr")
> > > lapply(x, library, character.only = TRUE)
> > > # From
> > https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
> > > states <- readOGR(dsn = "./cb_2014_us_state_5m.shp",
> > > layer = "cb_2014_us_state_5m", verbose =
> FALSE)
> > > # Make a copy of the SPDF attribute table, and then work
> > normally, as
> > > with any data.frame/data.table object
> > > states.df <- states at data
> > > # Create an explicit attribute to keep polygons IDs (useful to
> > > "re-attach" the table to the polygons later)
> > > states.df <- states.df %>% mutate(rn=row.names(states))
> > >
> > > # join them
> > > states.df <- full_join(x = states.df, y = counts, by="STUSPS")
> %>%
> > > filter(!(STUSPS%in%c("PR", "na", "MH", "FM", "PW"))) %>%
> > na.omit() %>%
> > > mutate(popup=paste0('<b>',NAME,'</b>','<br>',
> > > '<b>count:</b> ',
> > > prettyNum(count,big.mark=",",scientific=FALSE)))
> >
> > at this stage, you have:
> >
> > > dim(states at data)
> > [1] 56 9
> > > dim(states.df)
> > [1] 55 12
> >
> > >
> > > # Re-attach the attribute table to the SPDF
> > > states at data <- states.df
> >
> > now, you created an invalid object:
> > > length(geometry(states))
> > [1] 56
> > > dim(states at data)
> > [1] 55 12
> >
> > this confirms once more what has been said on this list so often,
> that
> > instead of using constructor functions such as
> > SpatialPolygonsDataFrame(), assigning slots directly is dangerous and
> > better left to those who know what they do (and check sanity):
> >
> > > states <- SpatialPolygonsDataFrame(geometry(states), states.df)
> > Error in SpatialPolygonsDataFrame(geometry(states), states.df) :
> > Object length mismatch:
> > geometry(states) has 56 Polygons objects, but states.df has 55
> rows
> >
> >
> > > # Make sure polygons IDs and data.frame row.names match
> > > states <- spChFIDs(states, states$rn)
> > >
> > > # Create map as usual...
> > >
> > > The error I get is:
> > >
> > > Error in spChFIDs(SP, x) : lengths differ
> > >
> > >
> > > Thanks for the help!
> > >
> > >
> > > Ignacio
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > _______________________________________________
> > > R-sig-Geo mailing list
> > > R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
> > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> > >
> >
> > --
> > Edzer Pebesma
> > Institute for Geoinformatics (ifgi), University of Münster,
> > Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
> > Journal of Statistical Software: http://www.jstatsoft.org/
> > Computers & Geosciences: http://elsevier.com/locate/cageo/
> > Spatial Statistics Society http://www.spatialstatistics.info
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
> --
> Edzer Pebesma
> Institute for Geoinformatics (ifgi), University of Münster,
> Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
> Journal of Statistical Software: http://www.jstatsoft.org/
> Computers & Geosciences: http://elsevier.com/locate/cageo/
> Spatial Statistics Society http://www.spatialstatistics.info
>
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list