Day 3Hw

churn <- read.csv("C:/Users/Gael Alvariza/Downloads/telecom_customer_churn.csv", header=TRUE)
pop <- read.csv("C:/Users/Gael Alvariza/Downloads/telecom_zipcode_population.csv", header=TRUE)

names(churn)
 [1] "Customer.ID"                       "Gender"                           
 [3] "Age"                               "Married"                          
 [5] "Number.of.Dependents"              "City"                             
 [7] "Zip.code"                          "Latitude"                         
 [9] "Longitude"                         "Number.of.Referrals"              
[11] "Tenure.in.Months"                  "Offer"                            
[13] "Phone.Service"                     "Avg.Monthly.Long.Distance.Charges"
[15] "Multiple.Lines"                    "Internet.Service"                 
[17] "Internet.Type"                     "Avg.Monthly.GB.Download"          
[19] "Online.Security"                   "Online.Backup"                    
[21] "Device.Protection.Plan"            "Premium.Tech.Support"             
[23] "Streaming.TV"                      "Streaming.Movies"                 
[25] "Streaming.Music"                   "Unlimited.Data"                   
[27] "Contract"                          "Paperless.Billing"                
[29] "Payment.Method"                    "Monthly.Charge"                   
[31] "Total.Charges"                     "Total.Refunds"                    
[33] "Total.Extra.Data.Charges"          "Total.Long.Distance.Charges"      
[35] "Total.Revenue"                     "Customer.Status"                  
[37] "Churn.Category"                    "Churn.Reason"                     
names(pop)
[1] "Zip.Code"   "Population"
dup_customer <- duplicated(churn$Customer.ID)
table(dup_customer)
dup_customer
FALSE 
 7043 
dup_zip <- duplicated(churn$Zip.code)
table(dup_zip)
dup_zip
FALSE  TRUE 
 1626  5417 
dup_zip_pop <- duplicated(pop$Zip.Code)
table(dup_zip_pop)
dup_zip_pop
FALSE 
 1671 
merged_data <- merge(churn, pop, by.x="Zip.code", by.y="Zip.Code", all.x=TRUE)

dim(merged_data)
[1] 7043   39
sum(is.na(merged_data$Population))
[1] 0
summary(merged_data$Age)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  19.00   32.00   46.00   46.51   60.00   80.00 
summary(merged_data$Population)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
     11    2344   17554   22140   36125  105285 
summary(merged_data$Monthly.Charge)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -10.00   30.40   70.05   63.60   89.75  118.75 
summary(merged_data$Total.Revenue)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
   21.36   605.61  2108.64  3034.38  4801.15 11979.34 
summary(merged_data$Tenure.in.Months)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    9.00   29.00   32.39   55.00   72.00 
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
zip_summary <- merged_data |>
  group_by(Zip.code) |>
  summarise(Avg_Population = mean(Population, na.rm = TRUE),
            Avg_Age = median(Age, na.rm = TRUE))

head(zip_summary, 10)
# A tibble: 10 × 3
   Zip.code Avg_Population Avg_Age
      <int>          <dbl>   <dbl>
 1    90001          54492    51  
 2    90002          44586    51  
 3    90003          58198    49  
 4    90004          67852    44  
 5    90005          43019    42.5
 6    90006          62784    41  
 7    90007          45025    42  
 8    90008          30852    42  
 9    90010           1957    26  
10    90011         101215    30  
library(stargazer)

Please cite as: 
 Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.3. https://CRAN.R-project.org/package=stargazer 
stargazer(merged_data,
          title = "Summary Stat",
          type = "text")

Summary Stat
=================================================================================
Statistic                           N      Mean     St. Dev.    Min       Max    
---------------------------------------------------------------------------------
Zip.code                          7,043 93,486.070 1,856.768   90,001    96,150  
Age                               7,043   46.510     16.750      19        80    
Number.of.Dependents              7,043   0.469      0.963       0         9     
Latitude                          7,043   36.197     2.469     32.556    41.962  
Longitude                         7,043  -119.757    2.154    -124.301  -114.193 
Number.of.Referrals               7,043   1.952      3.001       0         11    
Tenure.in.Months                  7,043   32.387     24.542      1         72    
Avg.Monthly.Long.Distance.Charges 6,361   25.421     14.200    1.010     49.990  
Avg.Monthly.GB.Download           5,517   26.190     19.587      2         85    
Monthly.Charge                    7,043   63.596     31.205   -10.000   118.750  
Total.Charges                     7,043 2,280.381  2,266.220   18.800  8,684.800 
Total.Refunds                     7,043   1.962      7.903     0.000     49.790  
Total.Extra.Data.Charges          7,043   6.861      25.105      0        150    
Total.Long.Distance.Charges       7,043  749.099    846.660    0.000   3,564.720 
Total.Revenue                     7,043 3,034.379  2,865.205   21.360  11,979.340
Population                        7,043 22,139.600 21,152.390    11     105,285  
---------------------------------------------------------------------------------