Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab04 - API Call #8

Open
ecking opened this issue Apr 3, 2020 · 8 comments
Open

Lab04 - API Call #8

ecking opened this issue Apr 3, 2020 · 8 comments
Labels

Comments

@ecking
Copy link

ecking commented Apr 3, 2020

Hello,

I'm in the first step on the lab and I have zero idea on what is wrong. I copied and pasted the code on the page... everything works. I go step by step using my state and it works until I get msp.pop section where all I'm doing is changing the code to "2."

I keep getting the error:

Getting data from the 2014-2018 5-year ACS Error: Your API call has errors. The API message returned is error: invalid 'in' argument.

Here is my code:

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

# search for citie names by strings, use the ^ anchor for "begins with" 

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

these.msp <- crosswalk$msaname == "ANCHORAGE,AK"
these.fips <- crosswalk$fipscounty[ these.msp ]
these.fips <- na.omit( these.fips )

library( tidycensus )


census_api_key("d91432ce26c09c5113ff80f733640aa68b0ebb36")


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

msp.pop <-
get_acs( geography = "tract", variables = "B01003_001",
         state = "2", county = county.fips[state.fips=="2"], geometry = TRUE ) %>% 
         select( GEOID, estimate ) %>%
         rename( POP=estimate )`
@ecking
Copy link
Author

ecking commented Apr 4, 2020

@Anthony-Howell-PhD I'm completely stumped. Is this what coding life is life? Just banging your head against a wall? haha

@lecy
Copy link
Contributor

lecy commented Apr 4, 2020

Did you try "02" instead of "2"?

I can't recall how this function is implemented, but FIPS codes sometimes require you to be precise.

This will give you a character vector:

state.fips <- substr( these.fips, 1, 2 )
head( state.fips )

So this is your problem I believe:

state.fips <- "02"
state.fips == "02"  # TRUE
state.fips == "2"   # FALSE

And yes, this is coding life! But you start to enjoy the puzzles after awhile. Turns work into a sudoku game :-)

@ecking
Copy link
Author

ecking commented Apr 4, 2020

I can do sudoku for hours. But one can only deal with commas and 1s and 0s for so long. lol.

That was it. :-|

Thank you, Dr. Lecy.

@Niagara1000
Copy link

image

Can someone please explain why I am getting this error? I want to talk about the San Jose-Santa Clara MSA for this assignment. This is just one of the many errors I have been getting all week D:

@Niagara1000
Copy link

image

Can someone please explain why I am getting this error? I want to talk about the San Jose-Santa Clara MSA for this assignment. This is just one of the many errors I have been getting all week D:

My name is Archana by the way. I can't edit my username

@lecy
Copy link
Contributor

lecy commented Apr 9, 2020

@Niagara1000 The 27 in the example corresponds to Minnesota. You need to use the proper state code and the counties within your city. See below.

Make sure you include a reproducible example with your question so there is enough information to answer:

Include the code up to the point where you receive an error (see below), and copy your code into the discussion by placing it between "fences":

```r
# code goes here
```

Use the Preview option above to check your question before you post to make sure it is formatting correct.

Reproducible example:

library( tidycensus )
# don't share your key on public boards
# key <- "abc123"
# census_api_key( key )

crosswalk <- read.csv( "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv",  stringsAsFactors=F, colClasses="character" )
these.msp <- crosswalk$msaname == "MINNEAPOLIS-ST. PAUL, MN-WI"
these.fips <- crosswalk$fipscounty[ these.msp ]
these.fips <- na.omit( these.fips )

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

unique( state.fips )
[1] "27" "55"

# Minneapolis-St.Paul is located 
# in two states so we need two calls,
# one for each state:
# state 27 = minnesota
# state 55 = wisconsin

counties.for.state.27 <- county.fips[ state.fips == "27" ]
counties.for.state.55 <- county.fips[ state.fips == "55" ]

# preview data
counties.for.state.27
 [1] "003" "019" "025" "037" "053" "059" "123" "139" "141" "163" "171"
counties.for.state.55
[1] "093" "109"

msp.pop1 <-
get_acs( geography = "tract", variables = "B01003_001",
         state = "27", county = counties.for.state.27, geometry = TRUE ) %>% 
         select( GEOID, estimate ) %>%
         rename( POP=estimate )

msp.pop2 <-
  get_acs( geography = "tract", variables = "B01003_001",
         state = "55", county = counties.for.state.55, geometry = TRUE ) %>% 
         select( GEOID, estimate ) %>%
         rename( POP=estimate )

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

@AntJam-Howell
Copy link
Collaborator

@Niagara1000 I've  posted lab 4 solutions on the course website: https://ds4ps.org/cpp-529-spr-2020/schedule/

The solutions (.RMD) provide a very concise code-through that shows how to finish most parts of the lab, including how to update the code to create the red dot plots using 3 variables instead of 30 variables as shown in the Lab instructions.   
STEPS:

  1. Please download the .RMD file, knit it before making any changes. Take a look at the code construction and compare it to what you have. That is the best way to learn where you are making mistakes.
  2. Once you've studied the code and compared it to what you were trying to do, the next step is to start making minor changes. There is actually only 2 very minor code manipulations that you need to make (shown below). After you make these changes, you will be able to knit and see the results for your own city. Note that I do not include the code to repeat the analysis using 15 variables (dim1, dim2, dim3), but it should be quite easy to extend.

Two Minor Changes :

  1. Change `MINNEAPOLIS-ST. PAUL, MN-WI' to your own chosen MSA city

these.msp <- crosswalk$msaname == "MINNEAPOLIS-ST. PAUL, MN-WI"

  1. Change the state code '27' to your own chosen state

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

@Niagara1000
Copy link

@lecy @Anthony-Howell-PhD
Thank you for your detailed responses. It was super helpful to fix the code.

-Archana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants