DS4PS / cpp-529-master

Course files for CPP 529 Data Analytics Practicum focused on models of neighborhood change.
https://ds4ps.org/cpp-529-master/
2 stars 1 forks source link

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

Open swest235 opened 5 months ago

swest235 commented 5 months ago

@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)
AntJam-Howell commented 5 months ago

Hi, It has to do with the merge. Please check this solution (link https://github.com/DS4PS/cpp-529-spr-2021/issues/29) and remove the leading zero in your dataframe to ensure the merge is successful. best

On Sat, Apr 6, 2024 at 1:45 AM swest235 @.***> wrote:

@AntJam-Howell https://urldefense.com/v3/__https://github.com/AntJam-Howell__;!!IKRxdwAv5BmarQ!aH1-eNm9T5wLK4DjlQDuKP_f1JwHo7HUMZOMrSZv-ZQdYA4c_nqggCYT59dvrTZfxOykc5BptiIuLWTtuocNh60XUu8GUA$ 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 https://urldefense.com/v3/__https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv__;!!IKRxdwAv5BmarQ!aH1-eNm9T5wLK4DjlQDuKP_f1JwHo7HUMZOMrSZv-ZQdYA4c_nqggCYT59dvrTZfxOykc5BptiIuLWTtuocNh63pxLR-uA$", 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 https://urldefense.com/v3/__https://github.com/DS4PS/cpp-529-master/raw/master/data/ltdb_std_2010_sample.rds__;!!IKRxdwAv5BmarQ!aH1-eNm9T5wLK4DjlQDuKP_f1JwHo7HUMZOMrSZv-ZQdYA4c_nqggCYT59dvrTZfxOykc5BptiIuLWTtuocNh62LSWbquA$" 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)

— Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/DS4PS/cpp-529-master/issues/32__;!!IKRxdwAv5BmarQ!aH1-eNm9T5wLK4DjlQDuKP_f1JwHo7HUMZOMrSZv-ZQdYA4c_nqggCYT59dvrTZfxOykc5BptiIuLWTtuocNh60NmS4jzQ$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AMK2Y7YDR2ZKOQPYVLMCW23Y36Y3XAVCNFSM6AAAAABF2IZJ6OVHI2DSMVQWIX3LMV43ASLTON2WKOZSGIZDSMJUG4ZDMNQ__;!!IKRxdwAv5BmarQ!aH1-eNm9T5wLK4DjlQDuKP_f1JwHo7HUMZOMrSZv-ZQdYA4c_nqggCYT59dvrTZfxOykc5BptiIuLWTtuocNh61rlB0HZQ$ . You are receiving this because you were mentioned.Message ID: @.***>

-- Anthony Howell (Website https://anthonyhowell.netlify.app/) Director, Center on Technology, Data, and Society, Associate Professor, Public Policy Arizona State University