DS4PS / cpp-529-spr-2021

Course shell for CPP 529 Community Analytics
http://ds4ps.org/cpp-529-spr-2021/
2 stars 0 forks source link

Lab 4 step 3 - merge removes most rows #12

Closed AprilPeck closed 2 years ago

AprilPeck commented 3 years ago

Whenever I run the merge command in Step 3, my table condenses to just 2 rows and doesn't look right when I map it. Here is my code, and an image of what is happening in my table.

crosswalk <- read.csv( "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv",  stringsAsFactors=F, colClasses="character" )

grep( "^FLAG", crosswalk$msaname, value=TRUE ) 

these.msp <- crosswalk$msaname == "FLAGSTAFF, ARIZONA-UTAH"
these.fips <- crosswalk$fipscounty[ these.msp ]
these.fips <- na.omit( these.fips )

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

flg.pop1 <-
get_acs( geography = "tract", variables = "B01003_001",
         state = "04", county = county.fips[state.fips=="04"], geometry = TRUE ) %>% 
         select( GEOID, estimate ) %>%
         rename( POP=estimate )

flg.pop2 <-
get_acs( geography = "tract", variables = "B01003_001",
         state = "49", county = county.fips[state.fips=="49"], geometry = TRUE ) %>% 
         select( GEOID, estimate ) %>%
         rename( POP=estimate )

flg.pop <- rbind( flg.pop1, flg.pop2 )

#Step 3
URL <- "https://github.com/DS4PS/cpp-529-master/raw/master/data/ltdb_std_2010_sample.rds"
census.dat <- readRDS(gzcon(url( URL )))

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

As you can see below, flg.pop1 and flg.pop2 make a combined total of 30 rows, which is reflected in flg.pop, but when I merge them into flg, it goes down to 2 rows. image

I tried running it for San Francisco, and when I did the merge it went down to 0 rows! image

AntJam-Howell commented 3 years ago

Hi April, I sent you a reply in your email. The solution is also posted in the previous open issue. Best,