Actividad 3

Carga de Datos

denue <- read_csv("denue_28.csv", locale = locale(encoding = "ISO-8859-1"))

1. Análisis descriptivo básico

glimpse(denue)
## Rows: 148,560
## Columns: 42
## $ id         <dbl> 8203497, 8011852, 6182392, 8178768, 8789487, 8443612, 61821…
## $ clee       <chr> "28002112512000023000000000U5", "28002114119000503000000000…
## $ nom_estab  <chr> "7 ADELANTE AGRICULTORES", "ACUACAM", "ACUACULTIVO EL TORAL…
## $ raz_social <chr> "7 ADELANTE AGRICULTORES S DE SS", "ACUACAM SA DE CV", "ACU…
## $ codigo_act <dbl> 112513, 112513, 112513, 112515, 112515, 112515, 112515, 112…
## $ nombre_act <chr> "Camaronicultura y acuicultura de otros crustáceos en agua …
## $ per_ocu    <chr> "0 a 5 personas", "0 a 5 personas", "6 a 10 personas", "0 a…
## $ tipo_vial  <chr> "CALLE", "CALLE", "CALLE", "CALLE", "OTRO(ESPECIFIQUE)", "O…
## $ nom_vial   <chr> "Sebastián Lerdo de Tejada", "CAMINO AL EJIDO PRESITAS", "N…
## $ tipo_v_e_1 <chr> "CALLE", "OTRO(ESPECIFIQUE)", "OTRO(ESPECIFIQUE)", "CALLE",…
## $ nom_v_e_1  <chr> "Xicoténcatl", "NINGUNO", "NINGUNO", "Ninguno", "GUADALUPE …
## $ tipo_v_e_2 <chr> "CALLE", "OTRO(ESPECIFIQUE)", "OTRO(ESPECIFIQUE)", "CALLE",…
## $ nom_v_e_2  <chr> "Ninguno", "NINGUNO", "NINGUNO", "Ninguno", "GENERAL FRANCI…
## $ tipo_v_e_3 <chr> "CALLE", "OTRO(ESPECIFIQUE)", "OTRO(ESPECIFIQUE)", "CALLE",…
## $ nom_v_e_3  <chr> "Reforma", "NINGUNO", "NINGUNO", "Ninguno", "LICENCIADO BEN…
## $ numero_ext <dbl> 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 604, 404, 0, 0, 200, 0…
## $ letra_ext  <chr> "SN", "SN", "DOMICILIO CONOCIDO", "SN", "SN", "DOMICILIO CO…
## $ edificio   <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ edificio_e <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ numero_int <dbl> NA, NA, NA, 0, NA, NA, 0, NA, NA, 0, NA, NA, NA, 0, NA, NA,…
## $ letra_int  <chr> NA, NA, NA, NA, NA, NA, "SN", NA, NA, NA, NA, NA, NA, NA, N…
## $ tipo_asent <chr> "COLONIA", "EJIDO", "RANCHO", "LOCALIDAD", "LOCALIDAD", "RA…
## $ nomb_asent <chr> "ZONA CENTRO", "LA PRESITA", "EL RINCON DEL TORAL", "GUIA E…
## $ tipoCenCom <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ nom_CenCom <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ num_local  <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ cod_postal <dbl> 89670, NA, NA, 87760, NA, NA, 87760, NA, 87140, 87505, 8817…
## $ cve_ent    <dbl> 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,…
## $ entidad    <chr> "Tamaulipas", "Tamaulipas", "Tamaulipas", "Tamaulipas", "Ta…
## $ cve_mun    <chr> "002", "002", "002", "001", "001", "037", "001", "002", "00…
## $ municipio  <chr> "Aldama", "Aldama", "Aldama", "Abasolo", "Abasolo", "Soto l…
## $ cve_loc    <chr> "0001", "0001", "0146", "0002", "0019", "1033", "0001", "10…
## $ localidad  <chr> "Aldama", "Aldama", "Morón", "Abasolo (Abasolito)", "Guía d…
## $ ageb       <chr> "0456", "0441", "0653", "0406", "0406", "0894", "0586", "03…
## $ manzana    <chr> "003", "007", "001", "001", "800", "800", "003", "800", "80…
## $ telefono   <dbl> NA, NA, NA, NA, 8342660492, NA, NA, NA, 8333038620, NA, 867…
## $ correoelec <chr> NA, NA, NA, NA, NA, "SERVICIORC@HOTMAIL.COM", "ACUAMEX@HOTM…
## $ www        <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ tipoUniEco <chr> "Fijo", "Fijo", "Fijo", "Fijo", "Fijo", "Fijo", "Fijo", "Fi…
## $ latitud    <dbl> 22.91579, 22.92367, 22.73246, 24.04349, 23.99640, 23.74997,…
## $ longitud   <dbl> -98.07725, -98.07561, -97.86541, -98.38367, -98.42889, -97.…
## $ fecha_alta <chr> "2019-11", "2019-11", "2011-03", "2019-11", "2019-11", "201…
summary(denue)
##        id               clee            nom_estab          raz_social       
##  Min.   :  189301   Length:148560      Length:148560      Length:148560     
##  1st Qu.: 4113595   Class :character   Class :character   Class :character  
##  Median : 8071460   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 7607127                                                           
##  3rd Qu.:10269381                                                           
##  Max.   :11846170                                                           
##                                                                             
##    codigo_act      nombre_act          per_ocu           tipo_vial        
##  Min.   :112513   Length:148560      Length:148560      Length:148560     
##  1st Qu.:461212   Class :character   Class :character   Class :character  
##  Median :522110   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :581417                                                           
##  3rd Qu.:722516                                                           
##  Max.   :931810                                                           
##                                                                           
##    nom_vial          tipo_v_e_1         nom_v_e_1          tipo_v_e_2       
##  Length:148560      Length:148560      Length:148560      Length:148560     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##   nom_v_e_2          tipo_v_e_3         nom_v_e_3           numero_ext     
##  Length:148560      Length:148560      Length:148560      Min.   :    0.0  
##  Class :character   Class :character   Class :character   1st Qu.:    0.0  
##  Mode  :character   Mode  :character   Mode  :character   Median :  100.0  
##                                                           Mean   :  506.9  
##                                                           3rd Qu.:  428.0  
##                                                           Max.   :88888.0  
##                                                           NA's   :10560    
##   letra_ext           edificio          edificio_e          numero_int      
##  Length:148560      Length:148560      Length:148560      Min.   :   -7.00  
##  Class :character   Class :character   Class :character   1st Qu.:    0.00  
##  Mode  :character   Mode  :character   Mode  :character   Median :    0.00  
##                                                           Mean   :   96.68  
##                                                           3rd Qu.:    0.00  
##                                                           Max.   :89410.00  
##                                                           NA's   :95097     
##   letra_int          tipo_asent         nomb_asent         tipoCenCom       
##  Length:148560      Length:148560      Length:148560      Length:148560     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##   nom_CenCom         num_local           cod_postal       cve_ent  
##  Length:148560      Length:148560      Min.   :    0   Min.   :28  
##  Class :character   Class :character   1st Qu.:87459   1st Qu.:28  
##  Mode  :character   Mode  :character   Median :88500   Median :28  
##                                        Mean   :88379   Mean   :28  
##                                        3rd Qu.:89120   3rd Qu.:28  
##                                        Max.   :99999   Max.   :28  
##                                        NA's   :4688                
##    entidad            cve_mun           municipio           cve_loc         
##  Length:148560      Length:148560      Length:148560      Length:148560     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##   localidad             ageb             manzana             telefono        
##  Length:148560      Length:148560      Length:148560      Min.   :1.000e+05  
##  Class :character   Class :character   Class :character   1st Qu.:8.333e+09  
##  Mode  :character   Mode  :character   Mode  :character   Median :8.361e+09  
##                                                           Mean   :8.535e+09  
##                                                           3rd Qu.:8.688e+09  
##                                                           Max.   :9.996e+09  
##                                                           NA's   :94742      
##   correoelec            www             tipoUniEco           latitud     
##  Length:148560      Length:148560      Length:148560      Min.   :22.21  
##  Class :character   Class :character   Class :character   1st Qu.:22.75  
##  Mode  :character   Mode  :character   Mode  :character   Median :25.82  
##                                                           Mean   :24.74  
##                                                           3rd Qu.:26.03  
##                                                           Max.   :27.64  
##                                                                          
##     longitud       fecha_alta       
##  Min.   :-99.77   Length:148560     
##  1st Qu.:-99.02   Class :character  
##  Median :-98.24   Mode  :character  
##  Mean   :-98.35                     
##  3rd Qu.:-97.85                     
##  Max.   :-97.15                     
## 

2 y 3. Total de registros y municipios

total_registros <- nrow(denue)
 
total_municipios <- denue %>% 
  summarise(n_municipios = n_distinct(municipio)) %>% 
  pull()
 
cat("Total de registros en Tamaulipas:", total_registros, "\n")
## Total de registros en Tamaulipas: 148560
cat("Total de municipios:", total_municipios, "\n")
## Total de municipios: 43

4. Total de sectores

denue <- denue %>%
  mutate(sector_id = str_sub(as.character(codigo_act), 1, 2))
 
total_sectores <- denue %>% 
  summarise(n_sectores = n_distinct(sector_id)) %>% 
  pull()
 
cat("Total de sectores económicos (a 2 dígitos):", total_sectores, "\n")
## Total de sectores económicos (a 2 dígitos): 23

5. Top 10 Sectores en Ciudad Victoria

top_10_victoria <- denue %>%
  filter(municipio == "Victoria") %>%
  count(nombre_act, name = "Total") %>%
  arrange(desc(Total)) %>%
  slice_head(n = 10)
 
top_10_victoria
## # A tibble: 10 × 2
##    nombre_act                                                              Total
##    <chr>                                                                   <int>
##  1 Comercio al por menor en tiendas de abarrotes, ultramarinos y miscelán…  1593
##  2 Salones y clínicas de belleza y peluquerías                               791
##  3 Restaurantes con servicio de preparación de antojitos                     549
##  4 Asociaciones y organizaciones religiosas                                  490
##  5 Reparación mecánica en general de automóviles y camiones                  456
##  6 Restaurantes con servicio de preparación de tacos y tortas                405
##  7 Comercio al por menor de artículos de papelería                           346
##  8 Comercio al por menor de ropa, excepto de bebé y lencería                 269
##  9 Purificación y embotellado de agua                                        259
## 10 Banca múltiple                                                            240

6 y 7. Tamaño de las empresas en la localidad

distribucion_victoria <- denue %>%
  filter(municipio == "Victoria") %>%
  count(per_ocu, name = "Total_Empresas") %>%
  arrange(desc(Total_Empresas))
 
distribucion_victoria
## # A tibble: 7 × 2
##   per_ocu            Total_Empresas
##   <chr>                       <int>
## 1 0 a 5 personas              13288
## 2 6 a 10 personas               997
## 3 11 a 30 personas              805
## 4 31 a 50 personas              216
## 5 51 a 100 personas             128
## 6 101 a 250 personas             97
## 7 251 y más personas             45
ggplot(distribucion_victoria, aes(x = fct_reorder(per_ocu, Total_Empresas), y = Total_Empresas)) +
  geom_col(fill = "#2c3e50") +
  coord_flip() + 
  theme_minimal() +
  labs(title = "Tamaño de Unidades Económicas en Ciudad Victoria",
       subtitle = "Basado en el rango de personal ocupado",
       x = "Rango de Personal Ocupado",
       y = "Número de Empresas")

8. Comparativa Regional

ciudades_comp <- c("Victoria", "Reynosa", "Matamoros", "Tampico", "Altamira")
 
comparativa_totales <- denue %>%
  filter(municipio %in% ciudades_comp) %>%
  count(municipio, name = "Total_Empresas") %>%
  arrange(desc(Total_Empresas))
 
comparativa_totales
## # A tibble: 5 × 2
##   municipio Total_Empresas
##   <chr>              <int>
## 1 Reynosa            26156
## 2 Matamoros          24969
## 3 Tampico            15655
## 4 Victoria           15576
## 5 Altamira            8889
# Preparar datos para el gráfico
grafico_comparativo <- denue %>%
  filter(municipio %in% ciudades_comp) %>%
  count(municipio, per_ocu, name = "Cantidad")
 
# Gráfico comparativo
ggplot(grafico_comparativo, aes(x = reorder(municipio, -Cantidad), y = Cantidad, fill = per_ocu)) +
  geom_col(position = "dodge") +
  theme_minimal() +
  labs(title = "Comparación de Tamaño de Empresas por Municipio",
       x = "Municipio",
       y = "Cantidad de Empresas",
       fill = "Personal Ocupado") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom")