day3_hw

Setup

library(knitr)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.1     ✔ readr     2.2.0
✔ ggplot2   4.0.3     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.2     ✔ tidyr     1.3.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

Import Data

churn <- read.csv("~/Downloads/telecom_customer_churn.csv")
dictionary <- read.csv("~/Downloads/telecom_data_dictionary.csv")
population <- read.csv("~/Downloads/telecom_zipcode_population.csv")

Merge

names(population) [names(population) == "Zip.Code"] <- "Zip.code"

merged <- merge(churn,
                population,
                by = "Zip.code",
                all.x = TRUE)

Analyze

zip_summary <- merged |>
  group_by(Zip.code) |>
  summarise(
    Avg_Population = mean(Population, na.rm = TRUE),
    Avg_Age = median(Age, na.rm = TRUE)
  ) |>
  tibble::column_to_rownames("Zip.code")

rownames(zip_summary) <- zip_summary$Zip.code

zip_summary <- zip_summary[, c("Avg_Age", "Avg_Population")]

knitr::kable(head(zip_summary),
             row.names = TRUE
             )
Avg_Age Avg_Population
1 51.0 54492
2 51.0 44586
3 49.0 58198
4 44.0 67852
5 42.5 43019
6 41.0 62784