#install.packages("readxl")
library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.1.8
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(dplyr)
library(ggplot2)
#file.choose()
ruta_excel <- "/Users/crisflorespalacios/Desktop/R Studio /Business Analytics/AbarrotesVentasBA.xlsx"
excel_sheets(ruta_excel)
## [1] "Total"
Abarrotes_Ventas<-read_excel(ruta_excel)
view(Abarrotes_Ventas)
Abarrotes_Ventas <- Abarrotes_Ventas %>%
mutate(`Monto Total` = Unidades * Precio)
Abarrotes_Ventas
## # A tibble: 200,620 × 23
## CveTienda DescG…¹ Codig…² PLU Fecha Hora Marca
## <chr> <chr> <dbl> <lgl> <dttm> <dttm> <chr>
## 1 MX001 Abarro… 7.50e12 NA 2020-06-19 08:16:20 1899-12-31 08:16:21 NUTR…
## 2 MX001 Abarro… 7.50e12 NA 2020-06-19 08:23:32 1899-12-31 08:23:33 DAN …
## 3 MX001 Abarro… 7.50e12 NA 2020-06-19 08:24:33 1899-12-31 08:24:33 BIMBO
## 4 MX001 Abarro… 7.50e12 NA 2020-06-19 08:24:33 1899-12-31 08:24:33 PEPSI
## 5 MX001 Abarro… 7.50e12 NA 2020-06-19 08:26:28 1899-12-31 08:26:28 BLAN…
## 6 MX001 Abarro… 7.50e12 NA 2020-06-19 08:26:28 1899-12-31 08:26:28 FLASH
## 7 MX001 Abarro… 7.50e12 NA 2020-06-19 08:26:28 1899-12-31 08:26:28 VARI…
## 8 MX001 Abarro… 7.50e12 NA 2020-06-19 08:26:28 1899-12-31 08:26:28 ZOTE
## 9 MX001 Abarro… 7.51e12 NA 2020-06-19 08:26:28 1899-12-31 08:26:28 ALWA…
## 10 MX001 Abarro… 3.22e10 NA 2020-06-19 15:24:02 1899-12-31 15:24:02 JUMEX
## # … with 200,610 more rows, 16 more variables: Fabricante <chr>,
## # Producto <chr>, Precio <dbl>, Ult_Costo <dbl>, Unidades <dbl>,
## # F_Ticket <dbl>, NombreDepartamento <chr>, NombreFamilia <chr>,
## # NombreCategoria <chr>, Estado <chr>, Mts2 <dbl>, TipoUbicación <chr>,
## # Giro <chr>, HoraInicio <dttm>, HoraCierre <dttm>, `Monto Total` <dbl>, and
## # abbreviated variable names ¹DescGiro, ²CodigoBarras
registros_erroneos <- subset(Abarrotes_Ventas, nchar(as.character(CodigoBarras)) != 13)
registros_erroneos
## # A tibble: 42,486 × 23
## CveTienda DescG…¹ Codig…² PLU Fecha Hora Marca
## <chr> <chr> <dbl> <lgl> <dttm> <dttm> <chr>
## 1 MX001 Abarro… 3.22e10 NA 2020-06-19 15:24:02 1899-12-31 15:24:02 JUMEX
## 2 MX001 Abarro… 7.50e 7 NA 2020-06-19 08:56:50 1899-12-31 08:56:50 COCA…
## 3 MX001 Abarro… 7.50e 7 NA 2020-06-19 15:44:06 1899-12-31 15:44:06 MARI…
## 4 MX001 Abarro… 7.50e 7 NA 2020-06-19 15:44:06 1899-12-31 15:44:06 COCA…
## 5 MX001 Abarro… 7.50e 7 NA 2020-06-19 09:07:17 1899-12-31 09:07:17 PERM…
## 6 MX001 Abarro… 7.50e 7 NA 2020-06-19 09:21:21 1899-12-31 09:21:21 COCA…
## 7 MX001 Abarro… 7.50e 7 NA 2020-06-19 09:21:21 1899-12-31 09:21:21 COCA…
## 8 MX001 Abarro… 7.50e 7 NA 2020-06-19 15:52:20 1899-12-31 15:52:21 CLOR…
## 9 MX001 Abarro… 7.50e 7 NA 2020-06-19 09:39:17 1899-12-31 09:39:17 COCA…
## 10 MX001 Abarro… 7.50e 7 NA 2020-06-19 16:02:00 1899-12-31 16:02:01 COCA…
## # … with 42,476 more rows, 16 more variables: Fabricante <chr>, Producto <chr>,
## # Precio <dbl>, Ult_Costo <dbl>, Unidades <dbl>, F_Ticket <dbl>,
## # NombreDepartamento <chr>, NombreFamilia <chr>, NombreCategoria <chr>,
## # Estado <chr>, Mts2 <dbl>, TipoUbicación <chr>, Giro <chr>,
## # HoraInicio <dttm>, HoraCierre <dttm>, `Monto Total` <dbl>, and abbreviated
## # variable names ¹DescGiro, ²CodigoBarras
Abarrotes_Ventas$CodigoBarras <- str_pad(Abarrotes_Ventas$CodigoBarras, width = 13, pad = "0")
view(Abarrotes_Ventas$CodigoBarras)
con_plu <- sum(!is.na(Abarrotes_Ventas$PLU))
sin_plu <- sum(is.na(Abarrotes_Ventas$PLU))
porcentaje_con_plu <- con_plu / nrow(Abarrotes_Ventas) * 100
cat("Porcentaje de registros con PLU: ", round(porcentaje_con_plu, 2), "%\n")
## Porcentaje de registros con PLU: 0.72 %
porcentaje_sin_plu <- sin_plu / nrow(Abarrotes_Ventas) * 100
cat("Porcentaje de registros sin PLU: ", round(porcentaje_sin_plu, 2), "%\n")
## Porcentaje de registros sin PLU: 99.28 %
nueva_base <- select(Abarrotes_Ventas, -PLU)
Abarrotes_Ventas <- subset(nueva_base)
negative_prices <- subset(Abarrotes_Ventas, Precio < 0)
nrow(negative_prices)
## [1] 147
Abarrotes_Ventas$Precio <- ifelse(Abarrotes_Ventas$Precio < 0, 0, Abarrotes_Ventas$Precio)
Abarrotes_Ventas %>%
group_by(CveTienda) %>% filter(Unidades >= 1) %>%
summarize(min_items = min(Unidades), max_items = max(Unidades), avg_items = mean(Unidades))
## # A tibble: 5 × 4
## CveTienda min_items max_items avg_items
## <chr> <dbl> <dbl> <dbl>
## 1 MX001 1 80 1.27
## 2 MX002 1 9 1.10
## 3 MX003 1 24 1.11
## 4 MX004 1 96 1.18
## 5 MX005 1 60 2.07
best_seller <- Abarrotes_Ventas %>%
group_by(CveTienda, Producto) %>%
summarize(total_units = sum(Unidades)) %>%
slice_max(total_units)
## `summarise()` has grouped output by 'CveTienda'. You can override using the
## `.groups` argument.
lowest_price <- Abarrotes_Ventas %>%
group_by(CveTienda, Producto) %>%
summarize(unit_price = min(Precio)) %>%
slice_head(n = 1)
## `summarise()` has grouped output by 'CveTienda'. You can override using the
## `.groups` argument.
highest_price <- Abarrotes_Ventas %>%
group_by(CveTienda, Producto) %>%
summarize(unit_price = max(Precio)) %>% arrange(desc(unit_price)) %>%
slice_head(n = 1)
## `summarise()` has grouped output by 'CveTienda'. You can override using the
## `.groups` argument.
best_seller
## # A tibble: 5 × 3
## # Groups: CveTienda [5]
## CveTienda Producto total_units
## <chr> <chr> <dbl>
## 1 MX001 Cerveza Tecate Light 340Ml 7476
## 2 MX002 Coca Cola Retornable 2.5L 329
## 3 MX003 Pepsi N.R. 1.5L 150
## 4 MX004 TECATE LIGHT LATA 340ML 1987
## 5 MX005 Cerveza Tecate Light 340Ml 7336
lowest_price
## # A tibble: 5 × 3
## # Groups: CveTienda [5]
## CveTienda Producto unit_price
## <chr> <chr> <dbl>
## 1 MX001 9PRINGLES EXTRA HOT PAPAS BOTE 40GR 14
## 2 MX002 ACONDICIONADOR CAPRICE ESPE 800ML 27
## 3 MX003 7500435108256 22
## 4 MX004 123 maxi efecto cn 900g 20
## 5 MX005 AZUCAR ESTANDAR 5 ESTRELLAS 500G 13
highest_price
## # A tibble: 5 × 3
## # Groups: CveTienda [5]
## CveTienda Producto unit_price
## <chr> <chr> <dbl>
## 1 MX001 Whisky Buchanan´s 1L 1000
## 2 MX002 NIDO KINDER 1 MAS 800G 145
## 3 MX003 Tocino Fud 250Grs 72
## 4 MX004 TEQUILA CAMPO AZUL 1 LTO 197
## 5 MX005 Six Tecate Light 355ML 90
ventas_mensuales <- Abarrotes_Ventas %>%
mutate(Mes = lubridate::month(Fecha)) %>%
group_by(CveTienda, Mes) %>%
summarise(Ventas = sum(Unidades * Precio)) %>%
ungroup()
## `summarise()` has grouped output by 'CveTienda'. You can override using the
## `.groups` argument.
view(ventas_mensuales)
# Gr??fico para el producto m??s vendido por tienda
ggplot(best_seller, aes(x = total_units, y = Producto, fill = CveTienda)) +
geom_col() +
labs(x = "Unidades vendidas", y = "Producto", fill = "Tienda") +
ggtitle("Producto m??s vendido por tienda")
# Gr??fico para el producto con el precio m??s bajo por tienda
ggplot(lowest_price, aes(x = unit_price, y = Producto, fill = CveTienda)) +
geom_col() +
labs(x = "Precio unitario", y = "Producto", fill = "Tienda") +
ggtitle("Producto con precio m??s bajo por tienda")
# Gr??fico para el producto con el precio m??s alto por tienda
ggplot(highest_price, aes(x = unit_price, y = Producto, fill = CveTienda)) +
geom_col() +
labs(x = "Precio unitario", y = "Producto", fill = "Tienda") +
ggtitle("Producto con precio m??s alto por tienda")
# Gr??fico de barras que muestre las ventas mensuales por tienda.
ggplot(ventas_mensuales, aes(x = Mes, y = Ventas, fill = CveTienda)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Mes", y = "Ventas ($)", fill = "Tienda") +
theme_minimal()
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.