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(stringr)
load("/cloud/project/data_parcial_1_2021.RData")
#Función para mostrar ventas anuales, trimestrales o mensuales
herramientas_mostrar_ventas<-function(){
data_parcial_1_2021 %>%
select(anio,
trimestre,
mes,
Sales,
Segment)->ventas
}
#Función para ver ventas anuales
herramientas_ver_ventasA<-function(){
data_parcial_1_2021 %>%
group_by(anio,
Sales,
Segment,
) %>%
summarise(`# de ventas anuales`=n())
}
#Función para ver ventas trimestrales
herramientas_ver_ventasT<-function(){
data_parcial_1_2021 %>%
group_by(trimestre,
Sales,
Segment,
) %>%
summarise(`# de ventas trimestrales`=n())
}
#Función para ver ventas mensuales
herramientas_ver_ventasM<-function(){
data_parcial_1_2021 %>%
group_by(mes,
Sales,
Segment,
) %>%
summarise(`# de ventas mensuales`=n())
}
# 1. Tabla en formato RAW.
anios_ranking<-2015:2018
tabla_top_k_raw<-function(df_comercio,top_k=5,anios_ranking,decimales=2
){
library(dplyr)
library(tidyr)
df_comercio %>%
filter(anio %in% anios_ranking) %>%
group_by(anio, State) %>%
summarise(`Total Ventas MM US$`=sum(Sales)) %>% mutate(percent=round(prop.table(`Total Ventas MM US$`)*100,2)) %>%
slice_max(n = 5,order_by = `Total Ventas MM US$`) %>%
as.data.frame() %>%
group_by(anio) %>%
mutate(rank = row_number(),
data=paste("CA","|",percent,sep = "")) %>%
select(anio, data, rank) %>% as.data.frame() %>% rename(`Año`=anio) %>%
pivot_wider(names_from = rank,values_from = data)->mi_tabla
print(mi_tabla)
}
Ejemplo de la Primer función:
tabla_top_k_raw(df_comercio = ventas,
anios_ranking =2015:2018,
top_k = 8,
decimales = 2 )
## # A tibble: 4 × 6
## Año `1` `2` `3` `4` `5`
## <dbl> <chr> <chr> <chr> <chr> <chr>
## 1 2015 CA|18.65 CA|13.41 CA|10.45 CA|7.13 CA|6.23
## 2 2016 CA|18.64 CA|16.85 CA|7.4 CA|4.46 CA|4.35
## 3 2017 CA|21.08 CA|11.8 CA|6.83 CA|5.5 CA|4.55
## 4 2018 CA|20.04 CA|12.99 CA|9.08 CA|6.01 CA|5.91