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 packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.5     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ stringr 1.4.0
## ✔ tidyr   1.2.0     ✔ forcats 0.5.1
## ✔ readr   2.1.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(ggplot2)
#file.choose()
bd <- read_csv("/Users/danieltrevino/Documents/Quinto Semestre TEC/Bootcamp de Programación/Convertir y Filtras Datos/arka1.csv")
## Warning: One or more parsing issues, see `problems()` for details
## Rows: 212951 Columns: 25
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): Territorio, Sub.Territorio, CEDI, Nombre, Tamaño.Cte.Industria, Se...
## dbl  (7): ID, Año, Cliente, Febrero, Marzo, Abril, Diciembre
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

getwd()

Convertir variables

bd$Cliente <- as.integer(bd$Cliente)  
bd$Enero <- as.integer(bd$Enero)
bd$Febrero <- as.integer(bd$Febrero)
bd$Marzo <- as.integer(bd$Marzo)
bd$Abril <- as.integer(bd$Abril)
bd$Mayo <- as.integer(bd$Mayo)
bd$Junio <- as.integer(bd$Junio)
bd$Julio <- as.integer(bd$Julio)
bd$Agosto <- as.integer(bd$Agosto)
bd$Septiembree <- as.integer(bd$Septiembre)
bd$Octubre <- as.integer(bd$Octubre)
bd$Noviembre <- as.integer(bd$Noviembre)
bd$Diciembre <- as.integer(bd$Diciembre)
str(bd)
## spec_tbl_df [212,951 × 26] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ ID                  : num [1:212951] 1 2 3 4 5 6 7 8 9 10 ...
##  $ Año                 : num [1:212951] 2016 2016 2016 2016 2016 ...
##  $ Territorio          : chr [1:212951] "Guadalajara" "Guadalajara" "Guadalajara" "Guadalajara" ...
##  $ Sub.Territorio      : chr [1:212951] "Belenes" "Belenes" "Belenes" "Belenes" ...
##  $ CEDI                : chr [1:212951] "Suc. Belenes" "Suc. Belenes" "Suc. Belenes" "Suc. Belenes" ...
##  $ Cliente             : int [1:212951] 77737 77737 77737 77737 77737 77737 77737 77737 77737 77737 ...
##  $ Nombre              : chr [1:212951] "ABARR" "ABARR" "ABARR" "ABARR" ...
##  $ Tamaño.Cte.Industria: chr [1:212951] "Extra Grande" "Extra Grande" "Extra Grande" "Extra Grande" ...
##  $ Segmento.Det        : chr [1:212951] "Agua Mineral" "Agua Purificada" "Agua Purificada" "Agua Saborizada" ...
##  $ Marca               : chr [1:212951] "Topo Chico A.M." "Ciel Agua Purificada" "Ciel Agua Purificada" "Ciel Exprim" ...
##  $ Presentacion        : chr [1:212951] "600 ml NR" "1 Ltro. N.R." "1.5 Lts. NR" "600 ml NR" ...
##  $ Tamaño              : chr [1:212951] "Individual" "Individual" "Individual" "Individual" ...
##  $ Retornable_NR       : chr [1:212951] "No Retornable" "No Retornable" "No Retornable" "No Retornable" ...
##  $ Enero               : int [1:212951] NA NA NA NA NA NA 1 NA 3 NA ...
##  $ Febrero             : int [1:212951] NA 2 NA NA NA NA NA 1 3 NA ...
##  $ Marzo               : int [1:212951] NA 8 3 NA NA 1 NA NA 4 NA ...
##  $ Abril               : int [1:212951] NA 4 6 NA NA NA NA 1 4 NA ...
##  $ Mayo                : int [1:212951] NA 4 3 NA NA NA 0 NA 4 NA ...
##  $ Junio               : int [1:212951] NA 2 3 NA NA NA NA 1 4 0 ...
##  $ Julio               : int [1:212951] NA 2 3 NA NA NA 0 NA 4 NA ...
##  $ Agosto              : int [1:212951] NA 2 3 NA NA NA NA 1 7 NA ...
##  $ Septiembre          : num [1:212951] NA 2 3 NA NA NA NA 1 4 NA ...
##  $ Octubre             : int [1:212951] NA 2 3 NA NA NA 0 NA 3 NA ...
##  $ Noviembre           : int [1:212951] NA 4 3 NA 0 NA NA NA 1 NA ...
##  $ Diciembre           : int [1:212951] 1 2 3 1 NA NA NA NA 3 NA ...
##  $ Septiembree         : int [1:212951] NA 2 3 NA NA NA NA 1 4 NA ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   ID = col_double(),
##   ..   Año = col_double(),
##   ..   Territorio = col_character(),
##   ..   Sub.Territorio = col_character(),
##   ..   CEDI = col_character(),
##   ..   Cliente = col_double(),
##   ..   Nombre = col_character(),
##   ..   Tamaño.Cte.Industria = col_character(),
##   ..   Segmento.Det = col_character(),
##   ..   Marca = col_character(),
##   ..   Presentacion = col_character(),
##   ..   Tamaño = col_character(),
##   ..   Retornable_NR = col_character(),
##   ..   Enero = col_number(),
##   ..   Febrero = col_double(),
##   ..   Marzo = col_double(),
##   ..   Abril = col_double(),
##   ..   Mayo = col_number(),
##   ..   Junio = col_number(),
##   ..   Julio = col_number(),
##   ..   Agosto = col_number(),
##   ..   Septiembre = col_number(),
##   ..   Octubre = col_number(),
##   ..   Noviembre = col_number(),
##   ..   Diciembre = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
summary(bd)
##        ID              Año        Territorio        Sub.Territorio    
##  Min.   :     1   Min.   :2016   Length:212951      Length:212951     
##  1st Qu.: 53238   1st Qu.:2016   Class :character   Class :character  
##  Median :106476   Median :2017   Mode  :character   Mode  :character  
##  Mean   :106476   Mean   :2017                                        
##  3rd Qu.:159714   3rd Qu.:2017                                        
##  Max.   :212951   Max.   :2018                                        
##                                                                       
##      CEDI              Cliente         Nombre          Tamaño.Cte.Industria
##  Length:212951      Min.   :    4   Length:212951      Length:212951       
##  Class :character   1st Qu.: 2980   Class :character   Class :character    
##  Mode  :character   Median : 8029   Mode  :character   Mode  :character    
##                     Mean   :31298                                          
##                     3rd Qu.:74022                                          
##                     Max.   :99998                                          
##                     NA's   :1                                              
##  Segmento.Det          Marca           Presentacion          Tamaño         
##  Length:212951      Length:212951      Length:212951      Length:212951     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##  Retornable_NR          Enero            Febrero           Marzo       
##  Length:212951      Min.   : -17.00   Min.   :-11.00   Min.   :-32.00  
##  Class :character   1st Qu.:   1.00   1st Qu.:  1.00   1st Qu.:  1.00  
##  Mode  :character   Median :   2.00   Median :  2.00   Median :  3.00  
##                     Mean   :  11.18   Mean   : 10.08   Mean   : 11.65  
##                     3rd Qu.:   6.00   3rd Qu.:  6.00   3rd Qu.:  6.00  
##                     Max.   :5333.00   Max.   :986.00   Max.   :986.00  
##                     NA's   :113350    NA's   :110094   NA's   :110244  
##      Abril             Mayo             Junio             Julio        
##  Min.   :-70.00   Min.   : -23.00   Min.   : -45.00   Min.   : -42.00  
##  1st Qu.:  1.00   1st Qu.:   1.00   1st Qu.:   1.00   1st Qu.:   1.00  
##  Median :  3.00   Median :   3.00   Median :   3.00   Median :   3.00  
##  Mean   : 11.73   Mean   :  13.98   Mean   :  13.73   Mean   :  13.15  
##  3rd Qu.:  6.00   3rd Qu.:   7.00   3rd Qu.:   7.00   3rd Qu.:   6.00  
##  Max.   :993.00   Max.   :6759.00   Max.   :5829.00   Max.   :6735.00  
##  NA's   :108049   NA's   :105666    NA's   :103867    NA's   :107919   
##      Agosto          Septiembre         Octubre          Noviembre      
##  Min.   :-211.00   Min.   :-527.00   Min.   : -38.00   Min.   : -25.00  
##  1st Qu.:   1.00   1st Qu.:   1.00   1st Qu.:   1.00   1st Qu.:   1.00  
##  Median :   3.00   Median :   3.00   Median :   3.00   Median :   3.00  
##  Mean   :  13.35   Mean   :  13.13   Mean   :  13.37   Mean   :  12.94  
##  3rd Qu.:   7.00   3rd Qu.:   6.00   3rd Qu.:   7.00   3rd Qu.:   6.00  
##  Max.   :6065.00   Max.   :6509.00   Max.   :6326.00   Max.   :5319.00  
##  NA's   :107785    NA's   :108190    NA's   :109236    NA's   :110914   
##    Diciembre       Septiembree     
##  Min.   :-28.00   Min.   :-527.00  
##  1st Qu.:  1.00   1st Qu.:   1.00  
##  Median :  3.00   Median :   3.00  
##  Mean   : 13.18   Mean   :  13.13  
##  3rd Qu.:  7.00   3rd Qu.:   6.00  
##  Max.   :993.00   Max.   :6509.00  
##  NA's   :112916   NA's   :108190
Marca <- count(bd, Marca, sort = TRUE)

Encontrar los datos perdidos y los atípicos de las variables

# Datos perdidos
boxplot(bd$Julio, horizontal = TRUE)

boxplot(bd$Agosto, horizontal = TRUE)

boxplot(bd$Septiembre, horizontal = TRUE)

boxplot(bd$Octubre, horizontal = TRUE)

boxplot(bd$Noviembre, horizontal = TRUE)

boxplot(bd$Diciembre, horizontal = TRUE)

# Todas las variables cuentan con valores atipicos

SELECT

bd_meses = select(bd, CEDI, Enero:Junio)
head(bd_meses)
## # A tibble: 6 × 7
##   CEDI         Enero Febrero Marzo Abril  Mayo Junio
##   <chr>        <int>   <int> <int> <int> <int> <int>
## 1 Suc. Belenes    NA      NA    NA    NA    NA    NA
## 2 Suc. Belenes    NA       2     8     4     4     2
## 3 Suc. Belenes    NA      NA     3     6     3     3
## 4 Suc. Belenes    NA      NA    NA    NA    NA    NA
## 5 Suc. Belenes    NA      NA    NA    NA    NA    NA
## 6 Suc. Belenes    NA      NA     1    NA    NA    NA

FILTER

bd_filtrada <- filter(bd, Tamaño.Cte.Industria == "Grande")
bd_filtrada
## # A tibble: 0 × 26
## # … with 26 variables: ID <dbl>, Año <dbl>, Territorio <chr>,
## #   Sub.Territorio <chr>, CEDI <chr>, Cliente <int>, Nombre <chr>,
## #   Tamaño.Cte.Industria <chr>, Segmento.Det <chr>, Marca <chr>,
## #   Presentacion <chr>, Tamaño <chr>, Retornable_NR <chr>, Enero <int>,
## #   Febrero <int>, Marzo <int>, Abril <int>, Mayo <int>, Junio <int>,
## #   Julio <int>, Agosto <int>, Septiembre <dbl>, Octubre <int>,
## #   Noviembre <int>, Diciembre <int>, Septiembree <int>
tail(bd_filtrada)
## # A tibble: 0 × 26
## # … with 26 variables: ID <dbl>, Año <dbl>, Territorio <chr>,
## #   Sub.Territorio <chr>, CEDI <chr>, Cliente <int>, Nombre <chr>,
## #   Tamaño.Cte.Industria <chr>, Segmento.Det <chr>, Marca <chr>,
## #   Presentacion <chr>, Tamaño <chr>, Retornable_NR <chr>, Enero <int>,
## #   Febrero <int>, Marzo <int>, Abril <int>, Mayo <int>, Junio <int>,
## #   Julio <int>, Agosto <int>, Septiembre <dbl>, Octubre <int>,
## #   Noviembre <int>, Diciembre <int>, Septiembree <int>

ARRANGE

bd_arrange <- arrange(bd, CEDI)
bd_arrange
## # A tibble: 212,951 × 26
##        ID   Año Territorio  Sub.Territorio CEDI  Cliente Nombre Tamaño.Cte.Indu…
##     <dbl> <dbl> <chr>       <chr>          <chr>   <int> <chr>  <chr>           
##  1 184065  2018 Territorio  Sub Territorio CEDI       NA Nombre Tamaño Cte Indu…
##  2      1  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
##  3      2  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
##  4      3  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
##  5      4  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
##  6      5  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
##  7      6  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
##  8      7  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
##  9      8  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
## 10      9  2016 Guadalajara Belenes        Suc.…   77737 ABARR  Extra Grande    
## # … with 212,941 more rows, and 18 more variables: Segmento.Det <chr>,
## #   Marca <chr>, Presentacion <chr>, Tamaño <chr>, Retornable_NR <chr>,
## #   Enero <int>, Febrero <int>, Marzo <int>, Abril <int>, Mayo <int>,
## #   Junio <int>, Julio <int>, Agosto <int>, Septiembre <dbl>, Octubre <int>,
## #   Noviembre <int>, Diciembre <int>, Septiembree <int>
bd_arrange1 <- arrange(bd, Marca)
bd_arrange1
## # A tibble: 212,951 × 26
##        ID   Año Territorio  Sub.Territorio CEDI  Cliente Nombre Tamaño.Cte.Indu…
##     <dbl> <dbl> <chr>       <chr>          <chr>   <int> <chr>  <chr>           
##  1 184080  2018 Guadalajara Huentitán      Suc.…    2658 DIAZ   Extra Grande    
##  2 184081  2018 Guadalajara Huentitán      Suc.…    2658 DIAZ   Extra Grande    
##  3 184296  2018 Guadalajara Huentitán      Suc.…    2682 VARGA  Extra Grande    
##  4 184297  2018 Guadalajara Huentitán      Suc.…    2682 VARGA  Extra Grande    
##  5 184410  2018 Guadalajara Huentitán      Suc.…    2687 MARIA  Extra Grande    
##  6 184519  2018 Guadalajara Huentitán      Suc.…    2713 MARIA  Extra Grande    
##  7 184609  2018 Guadalajara Huentitán      Suc.…    2736 MIGUE  Extra Grande    
##  8 184772  2018 Guadalajara Huentitán      Suc.…    2751 BLANC  Extra Grande    
##  9 184985  2018 Guadalajara Huentitán      Suc.…    2797 J GIL  Extra Grande    
## 10 185104  2018 Guadalajara Huentitán      Suc.…    2859 MINIS  Extra Grande    
## # … with 212,941 more rows, and 18 more variables: Segmento.Det <chr>,
## #   Marca <chr>, Presentacion <chr>, Tamaño <chr>, Retornable_NR <chr>,
## #   Enero <int>, Febrero <int>, Marzo <int>, Abril <int>, Mayo <int>,
## #   Junio <int>, Julio <int>, Agosto <int>, Septiembre <dbl>, Octubre <int>,
## #   Noviembre <int>, Diciembre <int>, Septiembree <int>
bd_arrange2 <- arrange(bd, Presentacion)
bd_arrange2
## # A tibble: 212,951 × 26
##       ID   Año Territorio  Sub.Territorio CEDI   Cliente Nombre Tamaño.Cte.Indu…
##    <dbl> <dbl> <chr>       <chr>          <chr>    <int> <chr>  <chr>           
##  1     2  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  2     6  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  3    11  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  4    16  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  5    19  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  6    36  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  7    85  2016 Guadalajara Belenes        Suc. …   77796 MARIA  Extra Grande    
##  8    94  2016 Guadalajara Belenes        Suc. …   77796 MARIA  Extra Grande    
##  9   102  2016 Guadalajara Belenes        Suc. …   77796 MARIA  Extra Grande    
## 10   111  2016 Guadalajara Belenes        Suc. …   77796 MARIA  Extra Grande    
## # … with 212,941 more rows, and 18 more variables: Segmento.Det <chr>,
## #   Marca <chr>, Presentacion <chr>, Tamaño <chr>, Retornable_NR <chr>,
## #   Enero <int>, Febrero <int>, Marzo <int>, Abril <int>, Mayo <int>,
## #   Junio <int>, Julio <int>, Agosto <int>, Septiembre <dbl>, Octubre <int>,
## #   Noviembre <int>, Diciembre <int>, Septiembree <int>

RENAME

bd_rename <- rename(bd, Sub_territorio = Sub.Territorio)
bd_rename
## # A tibble: 212,951 × 26
##       ID   Año Territorio  Sub_territorio CEDI   Cliente Nombre Tamaño.Cte.Indu…
##    <dbl> <dbl> <chr>       <chr>          <chr>    <int> <chr>  <chr>           
##  1     1  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  2     2  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  3     3  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  4     4  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  5     5  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  6     6  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  7     7  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  8     8  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
##  9     9  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
## 10    10  2016 Guadalajara Belenes        Suc. …   77737 ABARR  Extra Grande    
## # … with 212,941 more rows, and 18 more variables: Segmento.Det <chr>,
## #   Marca <chr>, Presentacion <chr>, Tamaño <chr>, Retornable_NR <chr>,
## #   Enero <int>, Febrero <int>, Marzo <int>, Abril <int>, Mayo <int>,
## #   Junio <int>, Julio <int>, Agosto <int>, Septiembre <dbl>, Octubre <int>,
## #   Noviembre <int>, Diciembre <int>, Septiembree <int>

MUTATE

marcas <- group_by(bd, Marca)
ventas_de_las_marcas <- summarize(marcas, Primer_semestre_de_marca=sum(Enero, Febrero, Marzo, Abril, Mayo, Junio, na.rm=TRUE))
head(ventas_de_las_marcas)
## # A tibble: 6 × 2
##   Marca        Primer_semestre_de_marca
##   <chr>                           <int>
## 1 AdeS Frutal                         0
## 2 AdeS Lácteo                         0
## 3 Barista Bros                        0
## 4 Bebere                            749
## 5 Burn                             2997
## 6 Café Blak                          79

SUMMARISE

bd_promedio <- group_by(bd, Marca, Presentacion, Tamaño)
promedio_marcas <- summarize(bd_promedio, Promedio_primer_semestre=mean(Enero&Febrero&Marzo&Abril&Mayo&Junio, na.rm=TRUE))
## `summarise()` has grouped output by 'Marca', 'Presentacion'. You can override
## using the `.groups` argument.
head(promedio_marcas)
## # A tibble: 6 × 4
## # Groups:   Marca, Presentacion [6]
##   Marca        Presentacion    Tamaño     Promedio_primer_semestre
##   <chr>        <chr>           <chr>                         <dbl>
## 1 AdeS Frutal  200 ml Tetra    Individual                  NaN    
## 2 AdeS Frutal  946 ml NR Tetra Familiar                    NaN    
## 3 AdeS Lácteo  200 ml Tetra    Individual                  NaN    
## 4 AdeS Lácteo  946 ml NR Tetra Familiar                    NaN    
## 5 Barista Bros 250 ml. NR PET  Individual                  NaN    
## 6 Bebere       473 ml NR       Individual                    0.228