Due 2/9 by the start of class.

Working in R

This week you’re going to try pulling American Community Survey data from the census using tidycensus – this turns out to be a very useful resource so I want everyone to have some experience with it!

The goal is to create a dataset that helps us understand the prevalence of children in in limited English speaking households for all counties in Virginia.

  1. Start an instance of RStudio using the project file inside your learningR folder. Start a new script (saved in your scripts folder) that loads tidyverse and tidycensus and then does the following:

  2. Run the following code chunk to acquire estimates of the population of school-aged children (5 to 17) within each county/locality in Virginia from the ACS 2017-2021 survey. This will provide a denominator so you can generate a percent later.

popvars <- c(b59 = "B01001_004",
             b1014 = "B01001_005",
             b1517 = "B01001_006",
             g59 = "B01001_028",
             g1014 = "B01001_029",
             g1517 = "B01001_030")

pop517 <- get_acs(geography = "county",
                  state = "51",
                  year = 2021,
                  survey = "acs5",
                  var = popvars,
                  output = "tidy")

pop517 <- pop517 %>% 
  group_by(GEOID, NAME) %>% 
  summarize(pop517 = sum(estimate)) %>% 
  ungroup()
  1. Now write your own code to download data from Table B16003 on the number of children aged 5 to 17 in limited English speaking households for all counties in Virginia from the ACS 2017-2021 survey. It might help you to have a look at the table before hand (linked above) and to scan the explanation of this set of tables at Census Reporter.

    • Specifically, download the number who speak only English at home, the number who speak Spanish at home, the number who speak another Indo-European language at home, the numbr who speak and Asian or Pacific Islander language at home, and the number who speak another language at home.
    • The command detail_vars <- load_variables(2021, "acs5", cache = TRUE) will load a list of variables in the ACS as a data frame. You can View() the data frame and search for the B16003 variable IDs to find specific cell names. I would definitely start by creating a vector of variables to pull.
    • You don’t need to specify any counties; the default will be for all counties in a state if you ask for the county geography.
    • Use the argument output = "tidy" as in the population code chunk above.
  2. Get familiar with both newly downloaded data sets with some combination of glimpse or str, head or tail, and summary. What’s the smallest number of children aged 5-17 in a county? The largest?

  3. Create a new data frame that sums up the number of children in households across all language categoried provided (that is, generate the total number of children in each county in limited Enlish speaking households using the data you’ve already pulled).

  4. Join the population data to both of your language data sets (the one containing the totals and the one disaggregating by language categories) with the following command (change df to the names of your data frames):

df <- df %>% 
  left_join(pop517)
  1. For both data sets (the one containing totals for all counties and the one containing the number in each language category for all countes) generate a new variable called percent that is the percent of children in a county in a limited English speaking household/specific language speaking household.

  2. Practice using the dplyr functions to explore the data containing the total number of children in any non-English speaking household.

    1. Write code that prints only the data for Charlottesville and Albemarle to the console. What do you notice?
    2. Write code that arranges the data set by the percent of children in limited English speaking homes from highest to lowest and prints the first 10 observations to the console. What place has the highest percent?
    3. Write code that counts how many localities in Virginia have 5% or more of the children aged 5-17 in limited Enlish speaking households.
  3. Practice using dplyr functions to explore the data containing the numbero f children in non-English speaking households by language group.

    1. Write code that tells us the minimum, mean, and maximum percent of children speaking each of the given categories of languages across the Virginia counties.
    2. This one’s a bit harder… write code that (1) groups by county, then (2) creates a variable this is the maximum value of the percent variable within each county (this will identify the primary non-English language group spoken by school-aged children in that county, if any), then (3) ungroups the data frame, then (4) filters the observations to keep those for which the maximum value of the percent you just created is not zero (these are cases with no children in limited English speaking households) AND for which the maximum value of the percent equals the value in the percent column (this keeps only the most frequently spoken language group), then (4) counts the frequency of the language groups (defined in the variable called variable, unless you changed the column name along the way).

Save the script into the scripts folder of your learningR folder. When complete, submit this file to me via direct message on slack (give it a name like week3_mpc.R as I’ll be adding everyone’s to the same script folder in my own version of this folder!)

Working in Teams: Stepping Stones progress

For the coming week, teams should start the following (I’ll send more specific feedback to your initial work on Friday):

  1. For the pre-collected variables and the variable you started acquirig last week, continue revising a brief description of what the measures are, their potential usefulness, and possible policy contexts or changes that may be relevant to understanding the measures.
  2. Write an initial R script to download the second measure in their set and review city excel file for background and issues.
  3. Revise the script to download the first measure if needed (you’ll hear more from me) – things I’ll be looking for include file structure and explanatory comments as well as validation of code.
  4. I’ll give team-specific feedback on next steps around variables that aren’t obviously available online.

This work should be submitted to the stepping stones channel in slack so we can better learn from one another.

Artwork by @allison_horst

Artwork by @allison_horst