library(dplyr) # o library(tidyverse)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
setwd("D:/Data")
datos <- read.csv("database.csv", header = TRUE, sep = ";", dec = ".")
str(datos)
## 'data.frame': 38113 obs. of 83 variables:
## $ Vehicle.ID : int 26587 27705 26561 27681 27550 28426 27549 28425 27593 28455 ...
## $ Year : int 1984 1984 1984 1984 1984 1984 1984 1984 1984 1984 ...
## $ Make : chr "Alfa Romeo" "Alfa Romeo" "Alfa Romeo" "Alfa Romeo" ...
## $ Model : chr "GT V6 2.5" "GT V6 2.5" "Spider Veloce 2000" "Spider Veloce 2000" ...
## $ Class : chr "Minicompact Cars" "Minicompact Cars" "Two Seaters" "Two Seaters" ...
## $ Drive : chr "" "}" "" "" ...
## $ Transmission : chr "Manual 5-Speed" "Manual 5-Speed" "Manual 5-Speed" "Manual 5-Speed" ...
## $ Transmission.Descriptor : chr "" "" "" "" ...
## $ Engine.Index : chr "9001" "9005" "9002" "9006" ...
## $ Engine.Descriptor : chr "(FFS)" "(FFS) CA model" "(FFS)" "(FFS) CA model" ...
## $ Engine.Cylinders : chr "6" "6" "4" "4" ...
## $ Engine.Displacement : chr "2.5" "2.5" "2" "2" ...
## $ Turbocharger : chr "" "" "" "" ...
## $ Supercharger : chr "" "" "" "" ...
## $ Fuel.Type : chr "Regular" "Regular" "Regular" "Regular" ...
## $ Fuel.Type.1 : chr "Regular Gasoline" "Regular Gasoline" "Regular Gasoline" "Regular Gasoline" ...
## $ Fuel.Type.2 : chr "" "" "" "" ...
## $ City.MPG..FT1. : chr "17" "17" "18" "18" ...
## $ Unrounded.City.MPG..FT1. : chr "0" "0" "0" "0" ...
## $ City.MPG..FT2. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Unrounded.City.MPG..FT2. : chr "0" "0" "0" "0" ...
## $ City.Gasoline.Consumption..CD. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ City.Electricity.Consumption : num 0 0 0 0 0 0 0 0 0 0 ...
## $ City.Utility.Factor : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Highway.MPG..FT1. : int 24 24 25 25 17 17 13 13 20 19 ...
## $ Unrounded.Highway.MPG..FT1. : chr "0" "0" "0" "0" ...
## $ Highway.MPG..FT2. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Unrounded.Highway.MPG..FT2. : chr "0" "0" "0" "0" ...
## $ Highway.Gasoline.Consumption..CD. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Highway.Electricity.Consumption : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Highway.Utility.Factor : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Unadjusted.City.MPG..FT1. : chr "21" "21" "23" "23" ...
## $ Unadjusted.Highway.MPG..FT1. : chr "34" "34" "35" "35" ...
## $ Unadjusted.City.MPG..FT2. : chr "0" "0" "0" "0" ...
## $ Unadjusted.Highway.MPG..FT2. : chr "0" "0" "0" "0" ...
## $ Combined.MPG..FT1. : num 20 20 21 21 17 17 13 13 17 17 ...
## $ Unrounded.Combined.MPG..FT1. : chr "0" "0" "0" "0" ...
## $ Combined.MPG..FT2. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Unrounded.Combined.MPG..FT2. : chr "0" "0" "0" "0" ...
## $ Combined.Electricity.Consumption : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Combined.Gasoline.Consumption..CD. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Combined.Utility.Factor : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Annual.Fuel.Cost..FT1. : int 1750 1750 1650 1650 2050 2050 2700 2700 2050 2050 ...
## $ Annual.Fuel.Cost..FT2. : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Gas.Guzzler.Tax : chr "" "" "" "" ...
## $ Save.or.Spend..5.Year. : chr "-2000" "-2000" "-1500" "-1500" ...
## $ Annual.Consumption.in.Barrels..FT1.: chr "164.805" "164.805" "156.957.142.857.143" "156.957.142.857.143" ...
## $ Annual.Consumption.in.Barrels..FT2.: chr "0" "0" "0" "0" ...
## $ Tailpipe.CO2..FT1. : chr "-1" "-1" "-1" "-1" ...
## $ Tailpipe.CO2.in.Grams.Mile..FT1. : chr "444.35" "444.35" "423.190.476.190.476" "423.190.476.190.476" ...
## $ Tailpipe.CO2..FT2. : chr "-1" "-1" "-1" "-1" ...
## $ Tailpipe.CO2.in.Grams.Mile..FT2. : chr "0" "0" "0" "0" ...
## $ Fuel.Economy.Score : chr "-1" "-1" "-1" "-1" ...
## $ GHG.Score : int -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
## $ GHG.Score..Alt.Fuel. : int -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
## $ My.MPG.Data : chr "N" "N" "N" "N" ...
## $ X2D.Passenger.Volume : chr "74" "74" "0" "0" ...
## $ X2D.Luggage.Volume : chr "7" "7" "0" "0" ...
## $ X4D.Passenger.Volume : int 0 0 0 0 0 0 0 0 0 0 ...
## $ X4D.Luggage.Volume : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Hatchback.Passenger.Volume : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Hatchback.Luggage.Volume : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Start.Stop.Technology : chr "" "" "" "" ...
## $ Alternative.Fuel.Technology : chr "" "" "" "" ...
## $ Electric.Motor : chr "" "" "" "" ...
## $ Manufacturer.Code : chr "" "" "" "" ...
## $ Gasoline.Electricity.Blended..CD. : chr "False" "False" "False" "False" ...
## $ Vehicle.Charger : chr "" "" "" "" ...
## $ Alternate.Charger : chr "" "" "" "" ...
## $ Hours.to.Charge..120V. : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Hours.to.Charge..240V. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Hours.to.Charge..AC.240V. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Composite.City.MPG : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Composite.Highway.MPG : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Composite.Combined.MPG : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Range..FT1. : int 0 0 0 0 0 0 0 0 0 0 ...
## $ City.Range..FT1. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Highway.Range..FT1. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Range..FT2. : chr "" "" "" "" ...
## $ City.Range..FT2. : chr "0" "0" "0" "0" ...
## $ Highway.Range..FT2. : num 0 0 0 0 0 0 0 0 0 0 ...
## $ X : int NA NA NA NA NA NA NA NA NA NA ...
## $ X.1 : int NA NA NA NA NA NA NA NA NA NA ...
Make <- datos$Make
TDFMake <- table(Make)
tabla1 <- as.data.frame(TDFMake)
hi <- tabla1$Freq / sum(tabla1$Freq)
hi_porc <- round(hi * 100, 2)
tabla1 <- data.frame(tabla1, hi_porc)
tabla1$Grupo <- NA
for (i in 1:nrow(tabla1)) {
if (tabla1$Freq[i] < 10) {
tabla1$Grupo[i] <- "< 10"
} else if (tabla1$Freq[i] < 100) {
tabla1$Grupo[i] <- "10 - 99"
} else if (tabla1$Freq[i] < 500) {
tabla1$Grupo[i] <- "100 - 499"
} else if (tabla1$Freq[i] < 1000) {
tabla1$Grupo[i] <- "500 - 999"
} else if (tabla1$Freq[i] < 5000) {
tabla1$Grupo[i] <- "1000 - 4999"
} else if (tabla1$Freq[i] < 10000) {
tabla1$Grupo[i] <- "5000 - 9999"
} else {
tabla1$Grupo[i] <- "10000 o más"
}
}
resumen_grupo1 <- table(tabla1$Grupo)
barplot(resumen_grupo1,
main = "Gráfica No.1: Distribución de marcas vehiculares",
xlab = "Grupo de frecuencia",
ylab = "Cantidad de marcas",
col = "skyblue",
las = 1)
resumen_grupo1.1 <- aggregate(Freq ~ Grupo, data = tabla1, FUN = sum)
barplot(resumen_grupo1.1$Freq,
names.arg = resumen_grupo1.1$Grupo,
main = "Gráfica No.1.2: Distribución de marcas vehiculares",
xlab = "Grupo de frecuencia",
ylab = "Frecuencia total",
col = "blue",
las = 1,
ylim = c(0, max(resumen_grupo1.1$Freq) * 1.1))
porc_1.3 <- aggregate(hi_porc ~ Grupo, data = tabla1, FUN = sum)
barplot(porc_1.3$hi_porc,
names.arg = porc_1.3$Grupo,
main = "Gráfica No.1.3: Distribución porcentual de marcas vehiculares",
xlab = "Grupo de frecuencia",
ylab = "Porcentaje %",
col = "steelblue",
las = 1)
porc_1.4 <- aggregate(hi_porc ~ Grupo, data = tabla1, FUN = sum)
barplot(porc_1.4$hi_porc,
names.arg = porc_1.4$Grupo,
main = "Gráfica No.1.4: Porcentaje global de marcas por grupo",
xlab = "Grupo de frecuencia",
ylab = "Porcentaje %",
col = "darkgreen",
las = 1,
ylim = c(0, 100))
etiqueta_Tipo <- paste0(round(porc_1.4$hi_porc, 2), "%")
colores <- rev(heat.colors(nrow(porc_1.4)))
pie(porc_1.4$hi_porc,
main = "Gráfica No.1:\nDistribución de grupos de frecuencia de marcas",
radius = 0.9,
col = colores,
labels = etiqueta_Tipo)
legend("bottomleft", legend = porc_1.4$Grupo,
title = "Grupos",
cex = 0.7,
fill = colores)
Model <- datos$Model
TDFModel <- table(Model)
tabla2 <- as.data.frame(TDFModel)
hi <- tabla2$Freq/sum(tabla2$Freq)
sum(hi)
## [1] 1
hi_porc <- round(hi*100,2)
tabla2 <- data.frame(tabla2, hi_porc)
tabla2$Grupo <- NA
for (i in 1:nrow(tabla2)) {
if (tabla2$Freq[i] < 10) {
tabla2$Grupo[i] <- "< 10"
} else if (tabla2$Freq[i] < 100) {
tabla2$Grupo[i] <- "10 - 99"
} else if (tabla2$Freq[i] < 500) {
tabla2$Grupo[i] <- "100 - 499"
} else if (tabla2$Freq[i] < 1000) {
tabla2$Grupo[i] <- "500 - 999"
} else if (tabla2$Freq[i] < 5000) {
tabla2$Grupo[i] <- "1000 - 4999"
} else if (tabla2$Freq[i] < 10000) {
tabla2$Grupo[i] <- "5000 - 9999"
} else {
tabla2$Grupo[i] <- "10000 o más"
}
}
resumen_grupo2 <- table(tabla2$Grupo)
barras2.1 <- barplot(resumen_grupo2,
main = "Gráfica No.2: Distribución de modelos vehiculares",
xlab = "Grupo de frecuencia",
ylab = "Cantidad de modelos",
col = c("skyblue"),
las = 1)
#Gráfica 2.2 - Frecuencia global
resumen_grupo2.2 <- aggregate(Freq ~ Grupo, data = tabla2, FUN = sum)
print(resumen_grupo2.2)
## Grupo Freq
## 1 < 10 9261
## 2 10 - 99 25192
## 3 100 - 499 3660
barras2.2 <-barplot(resumen_grupo2.2$Freq,
names.arg = resumen_grupo2.2$Grupo,
main = "Gráfica No.2.2: Distribución de modelos vehiculares",
xlab = "Grupo de frecuencia",
ylab = "Frecuencia total",
col = "blue",
las = 1,
ylim = c(0, max(resumen_grupo2.2$Freq) * 1.1))
#Gráfica 2.3 - porcentaje local
porc_2.3 <- aggregate(hi_porc ~ Grupo, data = tabla2, FUN = sum)
barras2.3 <-barplot(porc_2.3$hi_porc,
names.arg = porc_2.3$Grupo,
main = "Gráfica No.2.3: Distribución porcentual de modelos vehiculares",
xlab = "Grupo de frecuencia",
ylab = "Porcentaje %",
col = "steelblue",
las = 1,)
#Gráfica 2.4 - porcentaje global
porc_2.4 <- aggregate(hi_porc ~ Grupo, data = tabla2, FUN = sum)
barras2.4 <- barplot(porc_2.4$hi_porc,
names.arg = porc_2.4$Grupo,
main = "Gráfica No.2.4: Distribución porcentual de modelos por grupo",
xlab = "Grupo de frecuencia",
ylab = "Porcentaje %",
col = "darkgreen",
las = 1,
ylim = c(0, 100))
etiqueta_Tipo <- paste0(round(porc_2.4$hi_porc, 2), "%")
colores <- rev(heat.colors(nrow(porc_2.4)))
pie(porc_2.4$hi_porc,
main = "Gráfica No.2:\nDistribución de grupos de frecuencia de modelos",
radius = 0.9,
col = colores,
labels = etiqueta_Tipo)
legend("bottomleft", legend = porc_2.4$Grupo,
title = "Grupos",
cex = 0.7,
fill = colores)
Drive <- datos$Drive
TDFDrive <- table(Drive)
TDFDrive
## Drive
## Cargo Type"
## 1188 438
## Passenger Type" }
## 311 1
## 2-Wheel Drive 4-Wheel Drive
## 369 1103
## 4-Wheel or All-Wheel Drive All-Wheel Drive
## 6545 2091
## Front-Wheel Drive Large Cars
## 13348 1
## Part-time 4-Wheel Drive Rear-Wheel Drive
## 176 12533
## Small Pickup Trucks Special Purpose Vehicles
## 1 2
## Standard Pickup Trucks 2WD Standard Pickup Trucks 4WD
## 3 3
tabla3.00 <- as.data.frame(TDFDrive)
hi <- tabla3.00$Freq/sum(tabla3.00$Freq)
sum(hi)
## [1] 1
hi_porc <- round(hi*100,2)
tabla3.00 <- data.frame(tabla3.00, hi_porc)
tabla3.00$Grupo <- NA
# Agrupación
for (i in 1:nrow(tabla3.00)) {
if (tabla3.00$Freq[i] < 10) {
tabla3.00$Grupo[i] <- "< 10"
} else if (tabla3.00$Freq[i] < 100) {
tabla3.00$Grupo[i] <- "10 - 99"
} else if (tabla3.00$Freq[i] < 500) {
tabla3.00$Grupo[i] <- "100 - 499"
} else if (tabla3.00$Freq[i] < 1000) {
tabla3.00$Grupo[i] <- "500 - 999"
} else if (tabla3.00$Freq[i] < 5000) {
tabla3.00$Grupo[i] <- "1000 - 4999"
} else if (tabla3.00$Freq[i] < 10000) {
tabla3.00$Grupo[i] <- "5000 - 9999"
} else {
tabla3.00$Grupo[i] <- "10000 o más"
}
}
#Gráfica 3.1 - Tipos de traccion vehicular (local)
resumen_grupo3.01 <- table(tabla3.00$Grupo)
barras3.1.1 <- barplot(resumen_grupo3.01,
main = "Gráfica No.3: Frecuencia de tipos de tracción vehicular",
xlab = "Tipos de tracción",
ylab = "Frecuencia",
col = c("skyblue"),
las = 1)
#Gráfica 3.2 - Tipos de tracción vehicular (Global)
resumen_grupo3.2 <- aggregate(Freq ~ Grupo, data = tabla3.00, FUN = sum)
print(resumen_grupo3.2)
## Grupo Freq
## 1 < 10 11
## 2 100 - 499 1294
## 3 1000 - 4999 4382
## 4 10000 o más 25881
## 5 5000 - 9999 6545
barras3.2 <-barplot(resumen_grupo3.2$Freq,
names.arg = resumen_grupo3.2$Grupo,
main = "Gráfica No.3.2: Frecuencia de tipos de tracción vehicular",
xlab = "Grupos de tipos de tracción",
ylab = "Frecuencia",
col = "blue",
las = 1,
ylim = c(0, max(resumen_grupo3.2$Freq) * 1.1))
#Gráfica 3.3 - Pocentaje local
porc_3.3 <- aggregate(hi_porc ~ Grupo, data = tabla3.00, FUN = sum)
barras3.3 <-barplot(porc_3.3$hi_porc,
names.arg = porc_3.3$Grupo,
main = "Gráfica No.3.3: Distribución porcentual de tipos de tracción vehicular",
xlab = "Grupo de frecuencia",
ylab = "Porcentaje %",
col = "steelblue",
las = 1,)
#Gráfica 3.4 - Porcentaje global
porc_3.4 <- aggregate(hi_porc ~ Grupo, data = tabla3.00, FUN = sum)
barras3.4 <- barplot(porc_3.4$hi_porc,
names.arg = porc_3.4$Grupo,
main = "Gráfica No.3.4: Distribución porcentual de tipos de tracción vehicular",
xlab = "Grupo de frecuencia",
ylab = "Porcentaje %",
col = "darkgreen",
las = 1,
ylim = c(0, 100))
etiqueta_Tipo <- paste0(round(porc_3.4$hi_porc, 2), "%")
colores <- rev(heat.colors(nrow(porc_3.4)))
pie(porc_3.4$hi_porc,
main = "Gráfica No.3:\nDistribución de grupos de frecuencia tipos de tracción",
radius = 0.9,
col = colores,
labels = etiqueta_Tipo)
legend("bottomleft", legend = porc_3.4$Grupo,
title = "Grupos",
cex = 0.7,
fill = colores)
Fuel.Type <- datos$Fuel.Type
TDFFuel.Type <- table(Fuel.Type)
TDFFuel.Type
## Fuel.Type
## CNG
## 2380 47
## Diesel Electricity
## 840 133
## Gasoline or E85 Gasoline or natural gas
## 860 16
## Gasoline or propane Midgrade
## 8 77
## Premium Premium and Electricity
## 9448 3
## Premium Gas or Electricity Premium or E85
## 14 39
## Regular Regular Gas and Electricity
## 24223 17
## S T
## 6 2
tabla4 <- as.data.frame(TDFFuel.Type)
hi <- tabla4$Freq/sum(tabla4$Freq)
sum(hi)
## [1] 1
hi_porc <- round(hi*100,2)
tabla4 <- data.frame(tabla4, hi_porc)
tabla4$Grupo <- NA
# Agrupación
tabla4 <- tabla4 %>%
mutate(Grupo = case_when(
Fuel.Type == "Diesel" ~ "Diesel",
Fuel.Type == "Electricity" ~ "Eléctrico",
Fuel.Type %in% c("Regular", "Regular Gas and Electricity") ~ "Gasolina regular",
Fuel.Type %in% c("Premium", "Premium and Electricity", "Premium Gas or Electricity", "Premium or E85") ~ "Gasolina premium",
TRUE ~ "Otros"
))
#Gráfica 4.1 - Tipos de combustible (Local)
resumen_grupo4 <- table(tabla4$Grupo)
barras4.1 <- barplot(resumen_grupo4,
main = "Gráfica No.4: Frecuencia de tipos de combustible",
xlab = "Tipos de combustible",
ylab = "Frecuencia",
col = c("skyblue"),
las = 1)
#Gráfica 4.2 - Tipos de combustible (Globlal)
resumen_grupo4.2 <- aggregate(Freq ~ Grupo, data = tabla4, FUN = sum)
print(resumen_grupo4.2)
## Grupo Freq
## 1 Diesel 840
## 2 Eléctrico 133
## 3 Gasolina premium 9504
## 4 Gasolina regular 24240
## 5 Otros 3396
barras4.2 <-barplot(resumen_grupo4.2$Freq,
names.arg = resumen_grupo4.2$Grupo,
main = "Gráfica No.4.2: Frecuencia de tipos de combustible",
xlab = "Tipos de cobustible",
ylab = "Frecuencia",
col = "blue",
las = 1,
ylim = c(0, max(resumen_grupo4.2$Freq) * 1.1))
#Gráfica 4.3 Porcentaje local
porc_4.3 <- aggregate(hi_porc ~ Grupo, data = tabla4, FUN = sum)
barras4.3 <-barplot(porc_4.3$hi_porc,
names.arg = porc_4.3$Grupo,
main = "Gráfica No.4.3: Distribución porcentual de tipos de combustible",
xlab = "Grupo de frecuencia",
ylab = "Porcentaje %",
col = "steelblue",
las = 1,)
#Gráfica 4.4 Pocentaje global
porc_4.4 <- aggregate(hi_porc ~ Grupo, data = tabla4, FUN = sum)
barras4.4 <- barplot(porc_4.4$hi_porc,
names.arg = porc_4.4$Grupo,
main = "Gráfica No.4.4: Distribución porcentual de tipos de combustible",
xlab = "Grupo de frecuencia",
ylab = "Porcentaje %",
col = "darkgreen",
las = 1,
ylim = c(0, 100))
etiqueta_Tipo <- paste0(round(porc_4.4$hi_porc, 2), "%")
colores <- rev(heat.colors(nrow(porc_4.4)))
pie(porc_4.4$hi_porc,
main = "Gráfica No.4:\nDistribución de grupos de frecuencia tipos de combustible",
radius = 0.9,
col = colores,
labels = etiqueta_Tipo)
legend("bottomleft", legend = porc_4.4$Grupo,
title = "Grupos",
cex = 0.7,
fill = colores)