html_document: 
 toc: TRUE 
 toc:_float: TRUE
 code_download: TRUE
 theme: cosmo
 

Instalar paquetes y llamar librerías

# install.packages("readxl") #Leer documentos de excel 
library(readxl)
# install.packages("tidyverse") # Manipulacion de datos 
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.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
# install.packages("ggplot2") #Generar graficos presentables 
library(ggplot2)

Actividad Individual 2

# # Importar la base de datos

#file.choose()
df <- read_excel("/Users/danielsalinas/Desktop/Datos Arca Continental Original.xlsx")
## Warning: Expecting numeric in N184066 / R184066C14: got a date
## Warning: Expecting numeric in O184066 / R184066C15: got a date
## Warning: Expecting numeric in P184066 / R184066C16: got a date
## Warning: Expecting numeric in Q184066 / R184066C17: got a date
## Warning: Expecting numeric in R184066 / R184066C18: got a date
## Warning: Expecting numeric in S184066 / R184066C19: got a date
## Warning: Expecting numeric in T184066 / R184066C20: got a date
## Warning: Expecting numeric in U184066 / R184066C21: got a date
## Warning: Expecting numeric in V184066 / R184066C22: got a date
## Warning: Expecting numeric in W184066 / R184066C23: got a date
## Warning: Expecting numeric in X184066 / R184066C24: got a date
## Warning: Expecting numeric in Y184066 / R184066C25: got a date
str(df)
## tibble [466,509 × 25] (S3: tbl_df/tbl/data.frame)
##  $ ID                  : num [1:466509] 1 2 3 4 5 6 7 8 9 10 ...
##  $ Año                 : num [1:466509] 2016 2016 2016 2016 2016 ...
##  $ Territorio          : chr [1:466509] "Guadalajara" "Guadalajara" "Guadalajara" "Guadalajara" ...
##  $ Sub Territorio      : chr [1:466509] "Belenes" "Belenes" "Belenes" "Belenes" ...
##  $ CEDI                : chr [1:466509] "Suc. Belenes" "Suc. Belenes" "Suc. Belenes" "Suc. Belenes" ...
##  $ Cliente             : chr [1:466509] "77737" "77737" "77737" "77737" ...
##  $ Nombre              : chr [1:466509] "ABARR" "ABARR" "ABARR" "ABARR" ...
##  $ Tamaño Cte Industria: chr [1:466509] "Extra Grande" "Extra Grande" "Extra Grande" "Extra Grande" ...
##  $ Segmento Det        : chr [1:466509] "Agua Mineral" "Agua Purificada" "Agua Purificada" "Agua Saborizada" ...
##  $ Marca               : chr [1:466509] "Topo Chico A.M." "Ciel Agua Purificada" "Ciel Agua Purificada" "Ciel Exprim" ...
##  $ Presentacion        : chr [1:466509] "600 ml NR" "1 Ltro. N.R." "1.5 Lts. NR" "600 ml NR" ...
##  $ Tamaño              : chr [1:466509] "Individual" "Individual" "Individual" "Individual" ...
##  $ Retornable_NR       : chr [1:466509] "No Retornable" "No Retornable" "No Retornable" "No Retornable" ...
##  $ Enero               : num [1:466509] NA NA NA NA NA NA 1 NA 3 NA ...
##  $ Febrero             : num [1:466509] NA 2 NA NA NA NA NA 1 3 NA ...
##  $ Marzo               : num [1:466509] NA 8 3 NA NA 1 NA NA 4 NA ...
##  $ Abril               : num [1:466509] NA 4 6 NA NA NA NA 1 4 NA ...
##  $ Mayo                : num [1:466509] NA 4 3 NA NA NA 0 NA 4 NA ...
##  $ Junio               : num [1:466509] NA 2 3 NA NA NA NA 1 4 0 ...
##  $ Julio               : num [1:466509] NA 2 3 NA NA NA 0 NA 4 NA ...
##  $ Agosto              : num [1:466509] NA 2 3 NA NA NA NA 1 7 NA ...
##  $ Septiembre          : num [1:466509] NA 2 3 NA NA NA NA 1 4 NA ...
##  $ Octubre             : num [1:466509] NA 2 3 NA NA NA 0 NA 3 NA ...
##  $ Noviembre           : num [1:466509] NA 4 3 NA 0 NA NA NA 1 NA ...
##  $ Diciembre           : num [1:466509] 1 2 3 1 NA NA NA NA 3 NA ...
## Revisar NAs en la base de datos
# Cuántos NAs tengo en la base de datos?
sum(is.na(df))
## [1] 3148501
# Cuántos NAs tengo por variable?
sapply(df,function(x) sum(is.na(x)))
##                   ID                  Año           Territorio 
##                    0                    0                    0 
##       Sub Territorio                 CEDI              Cliente 
##                    0                    0                    0 
##               Nombre Tamaño Cte Industria         Segmento Det 
##                    0                    0                    0 
##                Marca         Presentacion               Tamaño 
##                    0                    0                    0 
##        Retornable_NR                Enero              Febrero 
##                    0               233480               231213 
##                Marzo                Abril                 Mayo 
##               227420               224057               216910 
##                Junio                Julio               Agosto 
##               215753               223411               220242 
##           Septiembre              Octubre            Noviembre 
##               337314               338386               338460 
##            Diciembre 
##               341855
# Opción 1. Eliminar NAs
# df1 <- na.omit(df)

# Opción 2. Reemplazar NAs con CEROS
df1 <- df
df1[is.na(df1)] <- 0
sum(is.na(df1))
## [1] 0
# Opción 3. Reemplazar NAs con PROMEDIO
# df1 <- df
#df1$Enero[is.na(df1$Enero)]<- mean(df1$Enero, na.rm=TRUE)
boxplot(df1$Enero)

df1 <- df1[df1$Enero < 6000,]
# Adecuar nombres con gsub
names(df1) <- gsub(" ","_",names(df1))

#Muestra las Ventas de Enero a Junio por Cedi
df2<- select(df1,c(CEDI,Enero:Julio))

# Muestra los movimientos por cedi y tamaño de tienda
df3<- df1 %>% filter(Tamaño_Cte_Industria== "Grande")

# Ordena la base de datos por cedi, marca y presentación
 df4 <- df1 %>% arrange (CEDI, Marca, Presentacion)
 
# Agrega un campo calculado con las ventas del primer semestre y muestra las ventas del primer semestre por marca.
 
 df5 <- df1 %>% mutate(Ventas_Sem1 = Enero + Febrero + Marzo + Abril + Mayo + Junio)
 
 Ventas_Sem1_por_marca <- df5 %>% group_by(Marca) %>% summarise(Ventas_Sem1=sum(Ventas_Sem1))
 
#Obtén la media de las ventas del primer semestre agrupado por marca, presentación y tamaño.
 
 df6 <- df5 %>% group_by(Marca, Presentacion, Tamaño) %>% summarise(Ventas_Sem1=mean(Ventas_Sem1))
## `summarise()` has grouped output by 'Marca', 'Presentacion'. You can override
## using the `.groups` argument.
# Calcular medidas de tendencia central
 summary(df1)
##        ID              Año        Territorio        Sub_Territorio    
##  Min.   :     1   Min.   :2016   Length:466508      Length:466508     
##  1st Qu.:116628   1st Qu.:2017   Class :character   Class :character  
##  Median :233256   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:466508      Length:466508      Length:466508      Length:466508       
##  Class :character   Class :character   Class :character   Class :character    
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character    
##                                                                               
##                                                                               
##                                                                               
##  Segmento_Det          Marca           Presentacion          Tamaño         
##  Length:466508      Length:466508      Length:466508      Length:466508     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##  Retornable_NR          Enero             Febrero             Marzo         
##  Length:466508      Min.   : -19.000   Min.   : -11.000   Min.   : -32.000  
##  Class :character   1st Qu.:   0.000   1st Qu.:   0.000   1st Qu.:   0.000  
##  Mode  :character   Median :   0.000   Median :   0.000   Median :   0.000  
##                     Mean   :   4.951   Mean   :   4.829   Mean   :   5.729  
##                     3rd Qu.:   2.000   3rd Qu.:   2.000   3rd Qu.:   3.000  
##                     Max.   :5333.000   Max.   :4995.000   Max.   :5636.000  
##      Abril               Mayo             Junio              Julio         
##  Min.   : -70.000   Min.   :-106.00   Min.   :-211.000   Min.   : -60.000  
##  1st Qu.:   0.000   1st Qu.:   0.00   1st Qu.:   0.000   1st Qu.:   0.000  
##  Median :   0.000   Median :   0.00   Median :   0.000   Median :   0.000  
##  Mean   :   5.992   Mean   :   6.73   Mean   :   6.464   Mean   :   6.033  
##  3rd Qu.:   3.000   3rd Qu.:   3.00   3rd Qu.:   3.000   3rd Qu.:   3.000  
##  Max.   :6164.000   Max.   :6759.00   Max.   :6033.000   Max.   :6735.000  
##      Agosto           Septiembre          Octubre           Noviembre      
##  Min.   :-211.000   Min.   :-527.000   Min.   : -38.000   Min.   : -25.00  
##  1st Qu.:   0.000   1st Qu.:   0.000   1st Qu.:   0.000   1st Qu.:   0.00  
##  Median :   0.000   Median :   0.000   Median :   0.000   Median :   0.00  
##  Mean   :   6.235   Mean   :   3.625   Mean   :   3.674   Mean   :   3.57  
##  3rd Qu.:   3.000   3rd Qu.:   1.000   3rd Qu.:   1.000   3rd Qu.:   1.00  
##  Max.   :6065.000   Max.   :6509.000   Max.   :6326.000   Max.   :5319.00  
##    Diciembre       
##  Min.   : -28.000  
##  1st Qu.:   0.000  
##  Median :   0.000  
##  Mean   :   3.858  
##  3rd Qu.:   0.000  
##  Max.   :6182.000
# Colapsar meses en una columna 
 df7 <- gather(df1, Mes, Ventas, Enero:Diciembre)
 df7
## # A tibble: 5,598,096 × 15
##       ID   Año Territorio  Sub_Territorio CEDI         Cliente Nombre
##    <dbl> <dbl> <chr>       <chr>          <chr>        <chr>   <chr> 
##  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 
##  7     7  2016 Guadalajara Belenes        Suc. Belenes 77737   ABARR 
##  8     8  2016 Guadalajara Belenes        Suc. Belenes 77737   ABARR 
##  9     9  2016 Guadalajara Belenes        Suc. Belenes 77737   ABARR 
## 10    10  2016 Guadalajara Belenes        Suc. Belenes 77737   ABARR 
## # ℹ 5,598,086 more rows
## # ℹ 8 more variables: Tamaño_Cte_Industria <chr>, Segmento_Det <chr>,
## #   Marca <chr>, Presentacion <chr>, Tamaño <chr>, Retornable_NR <chr>,
## #   Mes <chr>, Ventas <dbl>

Tarea colaborativa 2

# Ventas por tipo de envase

df7 <- df5 %>% group_by(Retornable_NR) %>% summarise((Ventas_Sem1=sum(Ventas_Sem1)))

# Ventas de tamaño tipo familiar
df8<- df5 %>% filter(Tamaño== "Familiar")

# Ventas por presentación
df9<- select(df5,c(Presentacion,Ventas_Sem1))

# Ventas segundo semestre
df10 <- df5 %>% mutate(Ventas_Sem2 = Julio + Agosto + Septiembre + Octubre + Noviembre + Diciembre)

# Ventas segundo semestre
Ventas_Sem2_por_marca <- df10 %>% group_by(Marca) %>% summarise(Ventas_Sem2=sum(Ventas_Sem2))

# Ventas por presentación y tipo de envase
 df11 <- df10 %>% arrange (CEDI, Presentacion, Retornable_NR)

Actividad Individual 3

#Agregar una columna con el numero de Mes 
#meses <- c("Enero"= "01", "Febrero" = "02", "Marzo" = "03", "Abril" = "04", "Mayo" = "05", "Junio"= "06" , "Julio" = "07" , "Agosto" = "08", "Septiembre" = "09", "Octubre" = "10" , "Noviembre" = "11" , "Diciembre" = "12")

#df7$Numero_de_Mes <- meses[df7$Mes]

# Grafica el promedio de ventas por mes y año 

#ventas_totales <- df7 %>%
 # group_by(Año, Numero_de_Mes) %>%
 # summarise(ventas_totales = sum(Ventas)) %>%
 # arrange(Numero_de_Mes)



#ggplot(ventas_totales , aes(x=Numero_de_Mes, y=ventas_totales,group = Año , color=as.factor(Año))) + geom_line() + geom_point() + labs(x="Mes", y="Ventas Totales(QTY)" , color = "Año") 

Tarea colaborativa 3

Detectar datos adicionales

# Ventas por tipo de envase

df7 <- df5 %>% group_by(Retornable_NR) %>% summarise((Ventas_Sem1=sum(Ventas_Sem1)))
# Ventas de tamaño tipo familiar
df8<- df5 %>% filter(Tamaño== "Familiar")
# Ventas por presentación
df9<- select(df5,c(Presentacion,Ventas_Sem1))
# Ventas segundo semestre
df10 <- df5 %>% mutate(Ventas_Sem2 = Julio + Agosto + Septiembre + Octubre + Noviembre + Diciembre)
# Ventas segundo semestre por marca
Ventas_Sem2_por_marca <- df10 %>% group_by(Marca) %>% summarise(Ventas_Sem2=sum(Ventas_Sem2))
# Ventas por presentación y tipo de envase
 df11 <- df10 %>% arrange (CEDI, Presentacion, Retornable_NR)
 # Colapsar meses en una Columna
 df12 <- gather(df10, Mes, Ventas, Enero:Diciembre)

Actividad Individual 4

Tarea colaborativa 4

Actividad Individual 5

Evidencia