0

(This is my first attempt to use R, so I apologize for any stupidity. Also, please let me know if this is better posted in Stack Overflow.)

I'm trying to use the Microsoft building footprint dataset to make a map of buildings in my city (Cambridge, MA). I'm following this gist as a guide. These lines of code are giving me problems:

options(tigris_class = "sf", tigris_use_cache = TRUE)
zctas_national <- zctas(cb = TRUE)

selected_zip <- zctas_national %>%
filter(ZCTA5CE10 %in% c(02139))

When I try to print(selected_zip), I get nothing back. Or rather, I get a table that looks like this:

[1] ZCTA5CE10  AFFGEOID10 GEOID10    ALAND10    AWATER10   geometry
<0 rows> (or 0-length row.names)

If I replace 02139 with 85308 (the zip from the gist), everything is fine:

  ZCTA5CE10     AFFGEOID10 GEOID10  ALAND10 AWATER10
1     85308 8600000US85308   85308 44254346   872904 

Anyone know what's going on?

1 Answer 1

1

Bit of a guess but...

   filter(ZCTA5CE10 %in% c(02139))

will treat that 02139 as a number, drop the leading zero, and look for "2139", which doesn't exist. Try making it into a character value:

   filter(ZCTA5CE10 %in% c("02139"))

If this is your first use of R, go through some R tutorials that only use the base R packages (and dont use all the "tidyverse" packages) to get a sound understanding of the fundamentals like this. Walk before you run!

1
  • Thanks, this worked. I thought it may have been a problem with the leading zero. Not sure why I never tried making it a string. :)
    – Andrew M.
    Commented Oct 28, 2018 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.