[1] “C:\Users\andre\OneDrive\Documentos\AISAC\arcacontal.png”
# install.packages("readxl") # Leer documentos de excel
library(readxl)
# install.packages("tidyverse") # Manipulacion de datos
library(tidyverse)
# install.packages("ggplot2") # Generar graficos presentables
library(ggplot2)
#file.choose()
df <- read_excel("C:\\Users\\andre\\OneDrive\\Documentos\\AISAC\\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 ...
# Cuantos NAs tengo en la base de datos?
sum(is.na(df))
## [1] 3148501
# Cuanto 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
# Opcion 1. Eliminar NAs
# df1 <- na.omit(df)
# Opcion 2. Reemplazar NAs con CEROS
df1 <- df
df1 [is.na(df1)] <- 0
sum(is.na(df1))
## [1] 0
# Opcion 3. Reemplazar NAs con PROMEDIO
# df1 <- df
# df1$Enero[is.na(df1$Enero)] <- mean(df1$Enero, na.rm=TRUE)
boxplot(df1$Enero)
# eliminar renglon de los totales
df1 <- df1[df1$Enero <6000, ]
boxplot(df1$Enero)
# Adecuar nombres con gsub
names(df1) <- gsub(" ","_", names(df1))
# Muestra las ventas de enero a junio por CEDI:
df2 <- select(df1,c(CEDI,Enero:Junio))
# Muestra los movimientos por CEDI y tamano de tienda grande:
df3 <- df1 %>% filter(Tamaño_Cte_Industria == "Grande")
# Ordena la base de datos por CEDI, por mrca y por presentacion:
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))
# Obten la media de las ventas del primer semestre agrupado por marca, presentacion y tamano
df6 <- df5 %>%
group_by(Marca, Presentacion, Tamaño) %>%
summarise(Ventas_Sem1+mean(Ventas_Sem1))
## Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
## dplyr 1.1.0.
## ℹ Please use `reframe()` instead.
## ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
## always returns an ungrouped data frame and adjust accordingly.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `summarise()` has grouped output by 'Marca', 'Presentacion', 'Tamaño'. 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