Skip to content

Lab 4, msp.sp <- as_Spatial(msp) Error #32

Description

@SwestCodes

@AntJam-Howell I'm following the code in lab 04, but currently I am stuck at this error.

"Error in (function (cond) :
error in evaluating the argument 'x' in selecting a method for function 'addAttrToGeom': subscript out of bounds"

Where we use the code chunk: msp.sp <- as_Spatial (msp) I get the error:

Trying to trouble shoot is making me more confused. For reference, here is the code leading up to it.

library( geojsonio )   # read shapefiles
library( sp )          # work with shapefiles
library( sf )          # work with shapefiles - simple features format
library( mclust )      # cluster analysis 
library( tmap )        # theme maps
library( ggplot2 )     # graphing 
library( ggthemes )
library( dplyr )
library( pander )
crosswalk <- read.csv( "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv",  stringsAsFactors=F, colClasses="character" )

grep( "^SAN", crosswalk$msaname, value=TRUE ) 
grep( "^SAN D", crosswalk$msaname, value=TRUE ) 
grep("^CALI", crosswalk$msaname, value = TRUE)
these.msp <- crosswalk$msaname == "CALIFORNIA"
these.fips <- crosswalk$fipscounty[these.msp]
these.fips <- na.omit(these.fips)
key <- "ada48681b172e752512698d7c4a80ced317e9f34"
census_api_key(key, install = TRUE)

state.fips <- substr(these.fips, 1, 2)
county.fips <- substr(these.fips, 3, 5)

cbind(these.fips, state.fips, county.fips) %>% pander()
msp.pop1 <- 
  get_acs(geography = "tract", variables = "B01003_001", state = "06", county = county.fips[state.fips=="06"], geometry = TRUE) %>% select (GEOID, estimate) %>% 
  rename(POP=estimate)
msp.pop2 <-
  get_acs(geography = "tract", variables = "B01003_001", state = "06", county = county.fips[state.fips=="06"], geometry = TRUE) %>% select(GEOID, estimate) %>% rename(POP=estimate)
msp.pop <- rbind(msp.pop1,msp.pop2)
URL <- "https://github.com/DS4PS/cpp-529-master/raw/master/data/ltdb_std_2010_sample.rds"
census.dat <- readRDS(gzcon(url( URL )))

msp <- merge( msp.pop, census.dat, by.x="GEOID", by.y="tractid" )

msp <- msp[ ! st_is_empty( msp ) , ]
class(msp)
msp.sp <- as_Spatial(msp)

class(msp.sp)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions