In-class Exercises
Part 1: the Field Calculator
- Download today's data if you haven't already.
- Open queens_census_tracts_joined in QGIS.
- We are going to use the Field Calculator and Refactor functions of QGIS to clean up the attribute table and perform some calculations on it.
- Use the field calculator to divide renters by households. Put the result in a field called s_renters. The expression should look like this:
and make the field a Decimal number with Output field length 10 and Precision 8."renters" / "households"
- Use the field calculator to create a new field called medincome2 that is med_income but an integer rather than text. This is tricky because some of the values in med_income cannot be converted to numbers. You'll need to use an expression like this:
if( "med_income" = '-', NULL, to_int("med_income"))
- Turn off editing mode by clicking the pencil icon and save the changes to the shapefile when prompted.
- Refactor the attribute table (using Refactor in the search bar) to get rid of some fields we no longer need:
- owners
- renters
- med_income
Part 2: Modeling
In this part we will use the data we worked with in the previous part to make a simple model. We will attempt to make a model that can predict where there will be more pressure on renters due to Airbnb listings in their neighborhoods.
- Continue with the file from the previous part. If you did not complete the previous part, open the shapefile in part2_start.
- Open the Airbnb listings shapefile in airbnb_listings_20181001_queens and use Count points in polygons to determine how many Airbnb listings exist in each census tract.
- Using the field calculator, create two new fields. The new fields should scale each field using
scale_linear
.- First, med_income has a minimum value of 15474, maximum value of 151964, so we use this expression in the field calculator:
Add an s_ at the beginning of the new field name to indicate that it is scaled. In this case, med_income becomes s_income (since you can only use 10 characters in a name). s_income should be a decimal number field with length 10 and precision 8.scale_linear("med_income", 15474, 151964, 1, 0)
- Follow a similar process for NUMPOINTS (created when we calculated the Airbnb listings per tract):
You'll need to find the minimum and maximum values for NUMPOINTS. Make the resulting field s_airbnb, also a decimal number with length 10 and precision 8.scale_linear("NUMPOINTS", minimum points, maximum points, 0, 1)
- First, med_income has a minimum value of 15474, maximum value of 151964, so we use this expression in the field calculator:
- Create a graduated style for the tracts. For the column expression, add each of the scaled fields (s_airbnb, s_renters, s_income) together.
Part 3: Carto
- Zip the shapefile in airbnb_listings_20181001_queens.
- Log in to your Carto account, go to the Datasets section, and upload the zip file to your Carto account.
- Create a map with the uploaded dataset.
- Change the styles of the points:
- Style the points by value with the room_type column to make a categorized style.
- Style the points by value with the price column to make a graduated style.
- Publish the map and get the public link for it.