Librerías Utilizadas

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.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ stringr 1.4.1
## ✔ tidyr   1.2.1     ✔ forcats 0.5.2
## ✔ 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

¿El dataframe tiene NAs?

df <- read.csv("/Users/lishdz/Desktop/R/arca.csv")
df$Cliente <- as.integer(df$Cliente)
## Warning: NAs introduced by coercion
df$Enero <- as.integer(df$Enero)
## Warning: NAs introduced by coercion
df$Febrero <- as.integer(df$Febrero)
## Warning: NAs introduced by coercion
df$Marzo <- as.integer(df$Marzo)
## Warning: NAs introduced by coercion
df$Abril <- as.integer(df$Abril)
## Warning: NAs introduced by coercion
df$Mayo <- as.integer(df$Mayo)
## Warning: NAs introduced by coercion
df$Junio <- as.integer(df$Junio)
## Warning: NAs introduced by coercion
df$Julio <- as.integer(df$Julio)
## Warning: NAs introduced by coercion
df$Agosto <- as.integer(df$Agosto)
## Warning: NAs introduced by coercion
df$Septiembre <- as.integer(df$Septiembre)
## Warning: NAs introduced by coercion
df$Octubre <- as.integer(df$Octubre)
## Warning: NAs introduced by coercion
df$Noviembre <- as.integer(df$Noviembre)
## Warning: NAs introduced by coercion
df$Diciembre <- as.integer(df$Diciembre)
## Warning: NAs introduced by coercion
sum(is.na(df))
## [1] 3149804

El Dataframe tiene 3,149,804 NAs.

¿El dataframe tiene datos atípicos?

#Enero
boxplot(df$Enero, horizontal = TRUE)

#Febrero
boxplot(df$Febrero, horizontal = TRUE)

#Marzo
boxplot(df$Marzo, horizontal = TRUE)

#Abril
boxplot(df$Abril, horizontal = TRUE)

#Mayo
boxplot(df$Mayo, horizontal = TRUE)

#Junio
boxplot(df$Junio, horizontal = TRUE)

#Julio
boxplot(df$Julio, horizontal = TRUE)

#Agosto
boxplot(df$Agosto, horizontal = TRUE)

#Septiembre
boxplot(df$Septiembre, horizontal = TRUE)

#Octubre
boxplot(df$Octubre, horizontal = TRUE)

#Noviembre
boxplot(df$Noviembre, horizontal = TRUE)

#Diciembre
boxplot(df$Diciembre, horizontal = TRUE)

Sí, el dataframe tiene datos atípicos en todos los meses.

Visualización de Outliers

df %>%
  ggplot() +
  aes(x = Sub.Territorio, y = Enero) +
  geom_point()
## Warning: Removed 233552 rows containing missing values (geom_point).

Funciones de R

Select

select <- select(df, CEDI, Enero:Junio)
head(select) 
##           CEDI Enero Febrero Marzo Abril Mayo Junio
## 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

filter <- filter(df, Tamaño.Cte.Industria=="Grande")
head(filter, n=10)
##        ID  Año  Territorio Sub.Territorio         CEDI Cliente Nombre
## 1  374960 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 2  374961 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 3  374962 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 4  374963 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 5  374964 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 6  374965 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 7  374966 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 8  374967 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 9  374968 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
## 10 374969 2019 Guadalajara        Belenes Suc. Belenes    7657  FROYL
##    Tamaño.Cte.Industria     Segmento.Det                Marca   Presentacion
## 1                Grande     Agua Mineral    Ciel Mineralizada      600 ml NR
## 2                Grande     Agua Mineral      Topo Chico A.M.    1.5 Lts. NR
## 3                Grande     Agua Mineral      Topo Chico A.M.      600 ml NR
## 4                Grande  Agua Purificada Ciel Agua Purificada   1 Ltro. N.R.
## 5                Grande  Agua Purificada Ciel Agua Purificada    1.5 Lts. NR
## 6                Grande  Agua Purificada Ciel Agua Purificada      5 Lts. NR
## 7                Grande  Agua Purificada Ciel Agua Purificada      600 ml NR
## 8                Grande  Agua Saborizada          Ciel Exprim   1 Ltro. N.R.
## 9                Grande  Agua Saborizada          Ciel Exprim      600 ml NR
## 10               Grande Bebidas de Fruta       Delaware Punch 250 ml. NR PET
##        Tamaño Retornable_NR Enero Febrero Marzo Abril Mayo Junio Julio Agosto
## 1  Individual No Retornable     1      NA    NA     1    1    NA    NA     NA
## 2    Familiar No Retornable    NA       2     5     2    2     2    NA      2
## 3  Individual No Retornable     1       3     3     3    4     1     1      3
## 4  Individual No Retornable     8       2    23    13   21     8    15     19
## 5  Individual No Retornable    13      13    25    22   29    13    10     22
## 6    Familiar No Retornable     4       7    14    14   11    11     7     11
## 7  Individual No Retornable    NA      NA     3     5    3     3    NA      5
## 8  Individual No Retornable     1       1    NA     1   NA    NA    NA     NA
## 9  Individual No Retornable     1       1    NA     1   NA    NA    NA     NA
## 10 Individual No Retornable    NA      NA    NA    NA    1     1    NA      1
##    Septiembre Octubre Noviembre Diciembre
## 1          NA      NA        NA        NA
## 2          NA      NA        NA        NA
## 3          NA      NA        NA        NA
## 4          NA      NA        NA        NA
## 5          NA      NA        NA        NA
## 6          NA      NA        NA        NA
## 7          NA      NA        NA        NA
## 8          NA      NA        NA        NA
## 9          NA      NA        NA        NA
## 10         NA      NA        NA        NA

Arrange

dfAr <- arrange(df, df$CEDI, df$Marca, df$Presentacion) 
head(dfAr)
##       ID  Año  Territorio Sub.Territorio         CEDI Cliente Nombre
## 1 184065 2018  Territorio Sub Territorio         CEDI      NA Nombre
## 2 200110 2018 Guadalajara        Belenes Suc. Belenes    7800  SAN I
## 3 200224 2018 Guadalajara        Belenes Suc. Belenes    7875  ABARR
## 4 200461 2018 Guadalajara        Belenes Suc. Belenes    7917  ABARR
## 5 200576 2018 Guadalajara        Belenes Suc. Belenes    8010  ABARR
## 6 200682 2018 Guadalajara        Belenes Suc. Belenes    8186  SUPER
##   Tamaño.Cte.Industria    Segmento.Det       Marca Presentacion     Tamaño
## 1 Tamaño Cte Industria    Segmento Det       Marca Presentacion     Tamaño
## 2         Extra Grande Bebidas de Soya AdeS Frutal 200 ml Tetra Individual
## 3         Extra Grande Bebidas de Soya AdeS Frutal 200 ml Tetra Individual
## 4         Extra Grande Bebidas de Soya AdeS Frutal 200 ml Tetra Individual
## 5         Extra Grande Bebidas de Soya AdeS Frutal 200 ml Tetra Individual
## 6         Extra Grande Bebidas de Soya AdeS Frutal 200 ml Tetra Individual
##   Retornable_NR Enero Febrero Marzo Abril Mayo Junio Julio Agosto Septiembre
## 1 Retornable_NR    NA      NA    NA    NA   NA    NA    NA     NA         NA
## 2 No Retornable    NA      NA    NA    NA   NA    NA    NA     NA         NA
## 3 No Retornable    NA      NA    NA    NA   NA    NA    NA     NA         NA
## 4 No Retornable    NA      NA    NA    NA   NA    NA    NA     NA         NA
## 5 No Retornable    NA      NA    NA    NA   NA    NA    NA     NA         NA
## 6 No Retornable    NA      NA    NA    NA   NA    NA    NA     NA         NA
##   Octubre Noviembre Diciembre
## 1      NA        NA        NA
## 2      NA         1         1
## 3      NA         2        NA
## 4      NA         1        NA
## 5      NA         6         3
## 6      NA         6         3

Rename

names(df)[names(df)=="Sub Territorio"] <- "Sub_territorio"
head(df)
##   ID  Año  Territorio Sub.Territorio         CEDI Cliente Nombre
## 1  1 2016 Guadalajara        Belenes Suc. Belenes   77737  ABARR
## 2  2 2016 Guadalajara        Belenes Suc. Belenes   77737  ABARR
## 3  3 2016 Guadalajara        Belenes Suc. Belenes   77737  ABARR
## 4  4 2016 Guadalajara        Belenes Suc. Belenes   77737  ABARR
## 5  5 2016 Guadalajara        Belenes Suc. Belenes   77737  ABARR
## 6  6 2016 Guadalajara        Belenes Suc. Belenes   77737  ABARR
##   Tamaño.Cte.Industria    Segmento.Det                Marca   Presentacion
## 1         Extra Grande    Agua Mineral      Topo Chico A.M.      600 ml NR
## 2         Extra Grande Agua Purificada Ciel Agua Purificada   1 Ltro. N.R.
## 3         Extra Grande Agua Purificada Ciel Agua Purificada    1.5 Lts. NR
## 4         Extra Grande Agua Saborizada          Ciel Exprim      600 ml NR
## 5         Extra Grande Agua Saborizada            Ciel Mini 300 ML. NR PET
## 6         Extra Grande Agua Saborizada      Ciel Saborizada   1 Ltro. N.R.
##       Tamaño Retornable_NR Enero Febrero Marzo Abril Mayo Junio Julio Agosto
## 1 Individual No Retornable    NA      NA    NA    NA   NA    NA    NA     NA
## 2 Individual No Retornable    NA       2     8     4    4     2     2      2
## 3 Individual No Retornable    NA      NA     3     6    3     3     3      3
## 4 Individual No Retornable    NA      NA    NA    NA   NA    NA    NA     NA
## 5 Individual No Retornable    NA      NA    NA    NA   NA    NA    NA     NA
## 6 Individual No Retornable    NA      NA     1    NA   NA    NA    NA     NA
##   Septiembre Octubre Noviembre Diciembre
## 1         NA      NA        NA         1
## 2          2       2         4         2
## 3          3       3         3         3
## 4         NA      NA        NA         1
## 5         NA      NA         0        NA
## 6         NA      NA        NA        NA

Mutate

con_marcas <- group_by(df, Marca)
ventas_de_las_marcas <- summarize(con_marcas, PrimerSemestreDeMarca=sum(Enero, Febrero, Marzo, Abril, Mayo, Junio, na.rm=TRUE))
ventas_de_las_marcas
## # A tibble: 56 × 2
##    Marca                PrimerSemestreDeMarca
##    <chr>                                <int>
##  1 AdeS Frutal                           8126
##  2 AdeS Lácteo                           4413
##  3 Barista Bros                           334
##  4 Bebere                                1034
##  5 Burn                                  5070
##  6 Café Blak                               79
##  7 Ciel Agua Purificada                863355
##  8 Ciel Exprim                          33959
##  9 Ciel Mineralizada                    18043
## 10 Ciel Mini                             4942
## # … with 46 more rows

Summarise

#Por marca
PromMarcas <- group_by(df, Marca)
PromMarcasV <- summarize(PromMarcas,PromPrimerSemestre_Marcas=mean (Enero&Febrero&Marzo&Abril&Mayo&Junio, na.rm=TRUE))
PromMarcasV
## # A tibble: 56 × 2
##    Marca                PromPrimerSemestre_Marcas
##    <chr>                                    <dbl>
##  1 AdeS Frutal                             0.0654
##  2 AdeS Lácteo                             0.0434
##  3 Barista Bros                            0     
##  4 Bebere                                  0.190 
##  5 Burn                                    0.0646
##  6 Café Blak                               1     
##  7 Ciel Agua Purificada                    0.926 
##  8 Ciel Exprim                             0.998 
##  9 Ciel Mineralizada                       0.972 
## 10 Ciel Mini                               0.255 
## # … with 46 more rows
#Por presentacion
PromPres <- group_by(df, Presentacion)
PromPresV <- summarize(PromPres,PromPrimerSemestre_Presentacion=mean (Enero&Febrero&Marzo&Abril&Mayo&Junio, na.rm=TRUE))
PromPresV
## # A tibble: 57 × 2
##    Presentacion     PromPrimerSemestre_Presentacion
##    <chr>                                      <dbl>
##  1 1 Ltro Ret.                             NaN     
##  2 1 Ltro. N.R.                              0.989 
##  3 1 Ltro. Tetra                             0.947 
##  4 1.250 Lts NR                              0.999 
##  5 1.5 Lts. NR                               0.994 
##  6 1.5 Lts. Ret                              0.995 
##  7 1.750 Lts NR                              0.994 
##  8 100 ml NR Tetra                           0.0710
##  9 12 Oz. NR Pet                             0.957 
## 10 12 Oz. NR Vidrio                          0.9   
## # … with 47 more rows
#Por tamaño
PromTamano <- group_by(df, Tamaño)
PromTamanoV <- summarize(PromTamano,PromPrimerSemestre_Tamano=mean (Enero&Febrero&Marzo&Abril&Mayo&Junio, na.rm=TRUE))
PromTamanoV
## # A tibble: 3 × 2
##   Tamaño     PromPrimerSemestre_Tamano
##   <chr>                          <dbl>
## 1 Familiar                       0.966
## 2 Individual                     0.565
## 3 Tamaño                       NaN

Tendencias Centrales

#Media y Mediana
summary(df)
##        ID              Año        Territorio        Sub.Territorio    
##  Min.   :     1   Min.   :2016   Length:466509      Length:466509     
##  1st Qu.:116628   1st Qu.:2017   Class :character   Class :character  
##  Median :233255   Median :2018   Mode  :character   Mode  :character  
##  Mean   :233255   Mean   :2018                                        
##  3rd Qu.:349882   3rd Qu.:2019                                        
##  Max.   :466509   Max.   :2019                                        
##                                                                       
##      CEDI              Cliente         Nombre          Tamaño.Cte.Industria
##  Length:466509      Min.   :    3   Length:466509      Length:466509       
##  Class :character   1st Qu.: 2509   Class :character   Class :character    
##  Mode  :character   Median : 5488   Mode  :character   Mode  :character    
##                     Mean   :16768                                          
##                     3rd Qu.: 9267                                          
##                     Max.   :99998                                          
##                     NA's   :1                                              
##  Segmento.Det          Marca           Presentacion          Tamaño         
##  Length:466509      Length:466509      Length:466509      Length:466509     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##  Retornable_NR          Enero           Febrero           Marzo       
##  Length:466509      Min.   :-19.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   :  9.39   Mean   :  9.09   Mean   : 10.54  
##                     3rd Qu.:  6.00   3rd Qu.:  6.00   3rd Qu.:  6.00  
##                     Max.   :999.00   Max.   :986.00   Max.   :986.00  
##                     NA's   :233552   NA's   :231286   NA's   :227507  
##      Abril             Mayo             Junio             Julio       
##  Min.   :-70.00   Min.   :-106.00   Min.   :-211.00   Min.   :-60.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 :  2.00  
##  Mean   : 10.62   Mean   :  11.44   Mean   :  10.98   Mean   : 10.72  
##  3rd Qu.:  6.00   3rd Qu.:   7.00   3rd Qu.:   6.00   3rd Qu.:  6.00  
##  Max.   :993.00   Max.   : 991.00   Max.   : 998.00   Max.   :993.00  
##  NA's   :224186   NA's   :217073    NA's   :215908    NA's   :223538  
##      Agosto          Septiembre        Octubre         Noviembre     
##  Min.   :-211.00   Min.   :-527     Min.   :-38.0    Min.   :-25.0   
##  1st Qu.:   1.00   1st Qu.:   1     1st Qu.:  1.0    1st Qu.:  1.0   
##  Median :   3.00   Median :   3     Median :  3.0    Median :  3.0   
##  Mean   :  10.95   Mean   :  12     Mean   : 12.1    Mean   : 11.8   
##  3rd Qu.:   6.00   3rd Qu.:   7     3rd Qu.:  7.0    3rd Qu.:  6.0   
##  Max.   : 999.00   Max.   : 993     Max.   :998.0    Max.   :991.0   
##  NA's   :220367    NA's   :337402   NA's   :338483   NA's   :338546  
##    Diciembre     
##  Min.   :-28     
##  1st Qu.:  1     
##  Median :  3     
##  Mean   : 13     
##  3rd Qu.:  7     
##  Max.   :997     
##  NA's   :341955
#Moda
mode <- function(x){
  ux <- unique(x)
  ux[which.max(tabulate(match(x,ux)))]
}
#NOTA: Si ningun dato se repite la función pone el primer valor en lugar de marcar error. 
mode(df$ID)
## [1] 1
mode(df$Año)
## [1] 2019
mode(df$Territorio)
## [1] "Guadalajara"
mode(df$Sub.Territorio)
## [1] "Belenes"
mode(df$CEDI)
## [1] "Suc. Belenes"
mode(df$Cliente)
## [1] 286
mode(df$Nombre)
## [1] "ABARR"
mode(df$Tamaño.Cte.Industria)
## [1] "Extra Grande"
mode(df$Segmento.Det)
## [1] "Sabores Regular"
mode(df$Marca)
## [1] "Coca-Cola"
mode(df$Presentacion)
## [1] "600 ml NR"
mode(df$Tamaño)
## [1] "Individual"
mode(df$Retornable_NR)
## [1] "No Retornable"
mode(df$Enero)
## [1] NA
mode(df$Febrero)
## [1] NA
mode(df$Marzo)
## [1] NA
mode(df$Abril)
## [1] NA
mode(df$Mayo)
## [1] NA
mode(df$Junio)
## [1] NA
mode(df$Julio)
## [1] NA
mode(df$Agosto)
## [1] NA
mode(df$Septiembre)
## [1] NA
mode(df$Octubre)
## [1] NA
mode(df$Noviembre)
## [1] NA
mode(df$Diciembre)
## [1] NA

Conclusiones

Con R es posible hacer un análisis y transformación de los datos muy profunda. Por ejemplo, se pudo identificar que la marca con mayores ventas durante el primer semestre del año fue ‘Ades Frutal’, esto se logró utilizando las funciones de summarize y sum. Igualmente se podría hacer una segmentación con la función de filter para medir las ventas por ciudad o por cada tienda de abarrotes individualmente.