Project 2 — Dataset 1: NYC Population by Borough

Author

Yalda Azamee

Published

November 28, 2026

Introduction

This dataset from the NYC Department of City Planning shows the population of New York City and its five boroughs every ten years from 1950 to 2040 (NYC Open Data: New York City Population by Borough, 1950–2040). It is a wide table: each year has its own column, and each year also has a second column with the borough’s share of the city total. Analysis question: Which boroughs grew or shrank since 1950, and how did each borough’s share of the city change?

Data Source

The file was downloaded from NYC Open Data as a CSV (Export → CSV) and saved without any changes as data/nyc_population_by_borough_raw.csv in my GitHub repository. The code below reads it from GitHub, so it runs on any computer.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
theme_set(theme_minimal(base_size = 12))  # same chart style in all three datasets

raw_url <- "https://raw.githubusercontent.com/YaldaAzamee/data607-project2/main/data/nyc_population_by_borough_raw.csv"

# check.names = FALSE keeps the original column names (e.g. "1950") unchanged
raw <- read.csv(raw_url, check.names = FALSE)

Data Structure Before Tidying

The raw data has 6 rows and 22 columns. The column names are:

names(raw)
 [1] "Age Group"                      "Borough"                       
 [3] "1950"                           "1950 - Boro share of NYC total"
 [5] "1960"                           "1960 - Boro share of NYC total"
 [7] "1970"                           "1970 - Boro share of NYC total"
 [9] "1980"                           "1980 - Boro share of NYC total"
[11] "1990"                           "1990 - Boro share of NYC total"
[13] "2000"                           "2000 - Boro share of NYC total"
[15] "2010"                           "2010 - Boro share of NYC total"
[17] "2020"                           "2020 - Boro share of NYC total"
[19] "2030"                           "2030 - Boro share of NYC total"
[21] "2040"                           "2040 - Boro share of NYC total"

A sample of the raw data (first 6 columns):

raw[, 1:6] |> knitr::kable()
Age Group Borough 1950 1950 - Boro share of NYC total 1960 1960 - Boro share of NYC total
Total Population NYC Total 7891957 100.00 7781984 100.00
Total Population Bronx 1451277 18.39 1424815 18.31
Total Population Brooklyn 2738175 34.70 2627319 33.76
Total Population Manhattan 1960101 24.84 1698281 21.82
Total Population Queens 1550849 19.65 1809578 23.25
Total Population Staten Island 191555 2.43 221991 2.85

The column names hold information that should be data: the year, and the measure (population or share of the city). This makes the table wide.

Transformation Steps

Step 1: Reshape from wide to long

All year columns are stacked into rows with pivot_longer(). names_pattern splits each column name into two parts: the first four digits are the year, and the rest of the name tells me the measure. A population column has nothing after the year, and a share column ends in " - Boro share of NYC total".

long <- raw |>
  pivot_longer(
    cols          = -c(`Age Group`, Borough),
    names_to      = c("year", "measure"),
    names_pattern = "^(\\d{4})(.*)$",
    values_to     = "value"
  )

head(long)
# A tibble: 6 × 5
  `Age Group`      Borough   year  measure                        value
  <chr>            <chr>     <chr> <chr>                          <dbl>
1 Total Population NYC Total 1950  ""                           7891957
2 Total Population NYC Total 1950  " - Boro share of NYC total"     100
3 Total Population NYC Total 1960  ""                           7781984
4 Total Population NYC Total 1960  " - Boro share of NYC total"     100
5 Total Population NYC Total 1970  ""                           7894862
6 Total Population NYC Total 1970  " - Boro share of NYC total"     100

Step 2: Normalize the variable structure

The measure column now contains two different kinds of values (people and percentages) in one value column. In tidy data each variable has its own column, so I give the measures short names and use pivot_wider() to put population and share in separate columns. I also convert year from text to an integer.

normalized <- long |>
  mutate(
    measure = if_else(measure == "", "population", "share_of_nyc_pct"),
    year    = as.integer(year)
  ) |>
  pivot_wider(names_from = measure, values_from = value)

head(normalized)
# A tibble: 6 × 5
  `Age Group`      Borough    year population share_of_nyc_pct
  <chr>            <chr>     <int>      <dbl>            <dbl>
1 Total Population NYC Total  1950    7891957              100
2 Total Population NYC Total  1960    7781984              100
3 Total Population NYC Total  1970    7894862              100
4 Total Population NYC Total  1980    7071639              100
5 Total Population NYC Total  1990    7322564              100
6 Total Population NYC Total  2000    8008278              100

Step 3: Rename variables

I rename all columns to lowercase snake_case, so every column follows the same naming convention.

renamed <- normalized |>
  rename(age_group = `Age Group`, borough = Borough)

names(renamed)
[1] "age_group"        "borough"          "year"             "population"      
[5] "share_of_nyc_pct"

Step 4: Handle missing and inconsistent values

# Missing values in each column
colSums(is.na(renamed))
       age_group          borough             year       population 
               0                0                0                0 
share_of_nyc_pct 
               0 
# Values in the age_group and borough columns
unique(renamed$age_group)
[1] "Total Population"
unique(renamed$borough)
[1] "NYC Total"        "   Bronx"         "   Brooklyn"      "   Manhattan"    
[5] "   Queens"        "   Staten Island"

Decisions:

  • Missing values: there are none, so no rows need to be removed.
  • Extra spaces: borough names start with spaces (for example " Bronx"). I remove them with str_trim().
  • age_group: it has only one value (“Total Population”), so it gives no information. I remove it.
  • “NYC Total” row: it is a sum of the boroughs, not a borough. I first check that the five boroughs add up to it, then remove it so it is not counted twice.
  • Projections: the dataset was published by City Planning before the 2020 Census, so the values for 2020, 2030, and 2040 are projections, not counts. I add a type column to mark each value as "Actual" or "Projected".
trimmed <- renamed |>
  mutate(borough = str_trim(borough)) |>
  select(-age_group)

# Check: do the five boroughs add up to the NYC total in each year?
check <- trimmed |>
  group_by(year) |>
  summarise(
    boroughs_sum = sum(population[borough != "NYC Total"]),
    nyc_total    = population[borough == "NYC Total"]
  ) |>
  mutate(difference = nyc_total - boroughs_sum)

check |>
  mutate(year = as.character(year)) |>  # so the year is not printed as "1,950"
  knitr::kable(format.args = list(big.mark = ","))
year boroughs_sum nyc_total difference
1950 7,891,957 7,891,957 0
1960 7,781,984 7,781,984 0
1970 7,894,862 7,894,862 0
1980 7,071,639 7,071,639 0
1990 7,322,564 7,322,564 0
2000 8,008,278 8,008,278 0
2010 8,242,624 8,242,624 0
2020 8,550,971 8,550,971 0
2030 8,821,027 8,821,027 0
2040 9,025,145 9,025,145 0
nyc_tidy <- trimmed |>
  filter(borough != "NYC Total") |>
  mutate(type = if_else(year >= 2020, "Projected", "Actual"))

head(nyc_tidy)
# A tibble: 6 × 5
  borough  year population share_of_nyc_pct type  
  <chr>   <int>      <dbl>            <dbl> <chr> 
1 Bronx    1950    1451277             18.4 Actual
2 Bronx    1960    1424815             18.3 Actual
3 Bronx    1970    1471701             18.6 Actual
4 Bronx    1980    1168972             16.5 Actual
5 Bronx    1990    1203789             16.4 Actual
6 Bronx    2000    1332650             16.6 Actual

The largest difference between the boroughs’ sum and the NYC total is 0 people, so the totals match. The final tidy dataset has 50 rows (5 boroughs × 10 years), and each row is one borough in one year.

Analytical Methods

All analysis uses the tidy dataset nyc_tidy. I compare each borough’s population in 1950 and 2010 (the last actual count) to calculate the change in people and in percent. I also compare each borough’s share of the city between those years. A line chart shows each borough over time, with projected years shown as dashed lines.

Analysis

change <- nyc_tidy |>
  filter(year %in% c(1950, 2010)) |>
  select(borough, year, population, share_of_nyc_pct) |>
  pivot_wider(names_from = year, values_from = c(population, share_of_nyc_pct)) |>
  mutate(
    change_people = population_2010 - population_1950,
    change_pct    = round(100 * change_people / population_1950, 1),
    share_change  = round(share_of_nyc_pct_2010 - share_of_nyc_pct_1950, 2)
  ) |>
  arrange(desc(change_pct))

change |>
  select(
    Borough                      = borough,
    `Population 1950 (people)`   = population_1950,
    `Population 2010 (people)`   = population_2010,
    `Change (people)`            = change_people,
    `Change (%)`                 = change_pct,
    `Share 1950 (% of NYC)`      = share_of_nyc_pct_1950,
    `Share 2010 (% of NYC)`      = share_of_nyc_pct_2010,
    `Share change (pct. points)` = share_change
  ) |>
  knitr::kable(format.args = list(big.mark = ","),
               caption = "Population change by borough, 1950–2010 (actual counts)")
Population change by borough, 1950–2010 (actual counts)
Borough Population 1950 (people) Population 2010 (people) Change (people) Change (%) Share 1950 (% of NYC) Share 2010 (% of NYC) Share change (pct. points)
Staten Island 191,555 468,730 277,175 144.7 2.43 5.69 3.26
Queens 1,550,849 2,250,002 699,153 45.1 19.65 27.30 7.65
Bronx 1,451,277 1,385,108 -66,169 -4.6 18.39 16.80 -1.59
Brooklyn 2,738,175 2,552,911 -185,264 -6.8 34.70 30.97 -3.73
Manhattan 1,960,101 1,585,873 -374,228 -19.1 24.84 19.24 -5.60
ggplot(nyc_tidy, aes(year, population / 1e6, color = borough)) +
  geom_line(data = filter(nyc_tidy, year <= 2020), linewidth = 1) +
  geom_line(data = filter(nyc_tidy, year >= 2010), linewidth = 1, linetype = "dashed") +
  geom_point(aes(shape = type), size = 2) +
  scale_x_continuous(breaks = seq(1950, 2040, 10)) +
  scale_y_continuous(breaks = seq(0, 3, 0.5)) +
  labs(
    title    = "NYC Population by Borough, 1950–2040",
    subtitle = "Dashed lines and open points are projections (2020–2040)",
    x        = "Year",
    y        = "Population (millions)",
    color    = "Borough",
    shape    = "Value type"
  ) +
  scale_shape_manual(values = c(Actual = 16, Projected = 1))

Conclusions

Findings

  • Staten Island grew the most: from 191,555 people in 1950 to 468,730 in 2010, an increase of 144.7%. It is still the smallest borough, at 5.69% of the city.
  • Manhattan lost the most: its population fell by 19.1%, and its share of the city dropped from 24.84% to 19.24%.
  • Queens gained the most share of the city (7.65 percentage points), while Manhattan, Brooklyn, and the Bronx all lost share. The city’s population moved away from Manhattan toward Queens and Staten Island.
  • The whole city shrank in the 1970s: the lowest total was in 1980 (7,071,639 people), after which every borough grew again.

AI Use

Anthropic. (2026). Claude Opus 5.5 [Large language model]. https://claude.ai. Accessed November 28, 2026. Used to help select the dataset, and understand the assignment requirements.