Las librerías
library(dplyr)
# install.packages("fdth") # Para Distribución de Frecuencias
library(fdth)
edades <- c(30, 20, 25, 40, 50)
edades
## [1] 30 20 25 40 50
Simular 1000 personas con edades Con la función sample() generamos números aleatorios set.seed(2020) Se establece para que genere los mismos números aleatorios
set.seed(2020)
edades <- sample(20:80, 1000, replace = TRUE)
edades
## [1] 47 63 42 41 43 20 36 55 61 25 68 64 75 27 61 69 35 73 48 53 67 22 21 48
## [25] 75 59 23 69 33 33 31 67 66 33 26 39 37 71 40 71 67 63 78 73 37 29 50 25
## [49] 66 62 32 51 79 43 32 64 21 50 35 32 20 57 22 48 44 64 62 44 24 62 77 57
## [73] 21 44 54 43 69 52 73 26 27 40 75 32 53 24 73 23 38 67 42 23 71 37 61 20
## [97] 66 41 50 60 79 35 25 43 74 45 29 45 52 47 20 36 59 34 63 77 69 32 24 43
## [121] 67 39 71 39 33 32 47 36 49 64 58 60 79 79 59 46 48 72 54 78 54 21 32 27
## [145] 42 80 51 23 58 65 68 74 72 55 43 67 60 79 48 47 73 21 51 36 36 56 29 70
## [169] 66 30 70 36 58 32 29 57 74 75 40 29 78 30 79 66 22 44 63 50 42 70 64 67
## [193] 23 40 79 80 60 56 71 75 78 22 77 45 73 20 48 33 43 46 58 37 55 38 44 77
## [217] 57 23 29 45 70 43 40 54 63 44 78 62 41 52 71 34 50 31 50 20 70 69 46 23
## [241] 22 39 78 43 39 63 48 20 80 58 63 63 41 76 31 31 80 29 32 46 64 61 60 63
## [265] 48 44 75 76 76 38 21 68 63 23 30 38 70 70 48 70 60 21 73 39 26 27 33 63
## [289] 44 55 42 29 70 32 46 54 46 73 46 39 42 26 47 24 71 46 58 72 41 78 40 44
## [313] 64 67 45 35 38 51 45 61 55 55 28 47 67 69 20 77 64 67 61 53 64 25 60 43
## [337] 70 22 66 69 45 73 75 57 53 53 23 59 28 62 29 62 61 31 76 60 49 51 75 79
## [361] 50 57 46 66 30 51 21 30 20 33 26 38 25 73 72 49 76 36 64 29 74 31 62 73
## [385] 51 30 78 68 57 44 47 49 49 31 37 62 26 26 77 25 25 45 55 79 58 29 77 39
## [409] 22 50 77 66 65 21 34 33 26 49 71 26 49 53 42 67 59 79 42 53 75 55 63 50
## [433] 76 29 36 51 54 57 24 73 33 59 38 66 75 72 38 40 41 72 80 34 43 25 44 32
## [457] 63 39 41 54 35 38 57 31 42 76 24 29 20 78 37 37 47 48 56 66 75 38 27 48
## [481] 40 56 56 72 32 52 72 64 29 73 38 25 63 48 20 43 64 26 22 45 67 80 23 78
## [505] 63 63 71 49 38 55 36 20 58 33 43 71 40 63 38 31 66 28 23 62 70 65 45 22
## [529] 56 22 65 22 80 72 38 72 72 61 77 49 70 54 31 76 71 38 41 74 51 22 62 29
## [553] 62 46 39 38 31 71 57 51 80 77 29 39 45 53 78 31 34 59 58 34 23 80 35 28
## [577] 74 70 27 22 55 26 32 57 29 45 21 46 70 77 45 77 25 68 21 66 21 74 34 30
## [601] 48 59 73 59 26 22 76 69 51 70 49 26 46 38 71 53 71 42 66 54 52 62 52 78
## [625] 40 69 29 37 53 26 31 51 35 74 74 80 28 62 28 57 68 64 55 49 57 35 78 63
## [649] 69 43 80 23 65 32 44 26 26 42 43 32 51 55 28 48 42 57 67 73 54 36 56 57
## [673] 71 65 39 22 51 59 45 41 51 43 76 36 48 77 44 51 64 68 72 49 31 47 27 40
## [697] 67 44 23 73 55 49 28 36 71 21 44 33 22 30 70 25 54 63 63 26 27 78 78 59
## [721] 48 65 60 32 47 36 53 22 24 44 41 43 20 64 69 32 28 33 71 46 23 59 77 23
## [745] 28 53 68 41 72 54 80 29 21 21 28 47 74 43 75 50 30 22 53 50 32 37 35 60
## [769] 28 63 74 52 28 61 66 69 67 54 32 33 77 60 76 33 61 80 68 80 25 68 37 45
## [793] 72 68 37 64 23 63 61 58 69 59 38 46 37 30 37 66 71 75 36 42 77 63 43 45
## [817] 50 33 57 54 53 25 44 69 72 69 39 71 45 76 72 37 46 65 39 50 68 30 51 69
## [841] 59 53 65 32 36 34 37 64 48 54 39 47 79 48 46 75 74 39 37 52 42 36 56 59
## [865] 67 55 44 66 67 73 57 77 43 43 55 57 23 80 44 69 52 22 60 29 26 72 42 80
## [889] 39 71 80 41 58 49 26 75 54 35 49 70 52 57 46 50 65 49 73 20 76 65 37 46
## [913] 79 33 61 23 68 69 75 20 20 60 75 56 72 45 22 79 41 21 69 76 55 72 43 73
## [937] 74 50 50 68 34 80 35 31 43 76 78 43 75 34 67 67 38 65 73 66 20 27 63 44
## [961] 76 34 52 28 38 38 66 56 60 75 78 52 36 24 64 55 54 33 36 30 52 46 23 27
## [985] 64 64 73 29 63 39 69 54 33 58 52 69 39 52 60 40
Determinar cuantas personas hay de cada edad Cuantas de 20 , 30, 45, 50, 80 …
frecuencias <- as.data.frame(table(edades))
colnames(frecuencias) <- c("Edades", "Frecuencia")
frecuencias <- arrange(frecuencias, desc(Frecuencia))
frecuencias
Tablas de Distribución de frecuencias con edades
tabla.distribucion <- fdt(edades, breaks = "Sturges")
tabla.distribucion
## Class limits f rf rf(%) cf cf(%)
## [19.8,25.35) 94 0.09 9.4 94 9.4
## [25.35,30.89) 76 0.08 7.6 170 17.0
## [30.89,36.44) 93 0.09 9.3 263 26.3
## [36.44,41.98) 82 0.08 8.2 345 34.5
## [41.98,47.53) 108 0.11 10.8 453 45.3
## [47.53,53.07) 97 0.10 9.7 550 55.0
## [53.07,58.62) 76 0.08 7.6 626 62.6
## [58.62,64.16) 100 0.10 10.0 726 72.6
## [64.16,69.71) 84 0.08 8.4 810 81.0
## [69.71,75.25) 109 0.11 10.9 919 91.9
## [75.25,80.8) 81 0.08 8.1 1000 100.0
Generar tabla de distribción del conjunto de datos coronavirus_confirmados.csv Cargar datos de manera local o desde la Web ??
datos <- read.csv("https://raw.githubusercontent.com/rpizarrog/Curso-Titulacion-Data-Science-/master/2020/datos/casos_confirmados.csv", encoding = "UTF-8", header = TRUE)
str(datos)
## 'data.frame': 19224 obs. of 6 variables:
## $ X : int 0 3 15 16 17 22 27 28 31 32 ...
## $ State : Factor w/ 32 levels "aguascalientes",..: 15 28 9 9 31 16 12 9 15 15 ...
## $ Sex : Factor w/ 2 levels "FEMENINO","MASCULINO": 1 2 2 1 2 1 1 2 1 1 ...
## $ Age : int 75 22 40 29 71 29 61 33 77 84 ...
## $ Date : Factor w/ 66 levels "2020-01-06","2020-01-08",..: 45 42 33 37 51 59 43 44 44 38 ...
## $ Confirmed: int 1 1 1 1 1 1 1 1 1 1 ...
summary(datos)
## X State Sex Age
## Min. : 0 distrito federal:5209 FEMENINO : 8039 Min. : 0.00
## 1st Qu.:22523 méxico :3130 MASCULINO:11185 1st Qu.: 35.00
## Median :44009 baja california :1557 Median : 46.00
## Mean :44045 tabasco : 984 Mean : 46.59
## 3rd Qu.:65793 sinaloa : 865 3rd Qu.: 57.00
## Max. :87365 quintana roo : 788 Max. :113.00
## (Other) :6691
## Date Confirmed
## 2020-04-20: 1144 Min. :1
## 2020-04-21: 1100 1st Qu.:1
## 2020-04-24: 1049 Median :1
## 2020-04-22: 1016 Mean :1
## 2020-04-23: 1006 3rd Qu.:1
## 2020-04-17: 937 Max. :1
## (Other) :12972
datos$State <- toupper(datos$State)
datos
Tabla de Frecuenca de la edad Age de los datos Coronavirus Confirmados
tabla.distribucion <- fdt(datos$Age, breaks = "Sturges")
tabla.distribucion
## Class limits f rf rf(%) cf cf(%)
## [0,7.13312) 132 0.01 0.69 132 0.69
## [7.13312,14.2662) 118 0.01 0.61 250 1.30
## [14.2662,21.3994) 330 0.02 1.72 580 3.02
## [21.3994,28.5325) 1646 0.09 8.56 2226 11.58
## [28.5325,35.6656) 2798 0.15 14.55 5024 26.13
## [35.6656,42.7987) 3070 0.16 15.97 8094 42.10
## [42.7987,49.9319) 3250 0.17 16.91 11344 59.01
## [49.9319,57.065) 3267 0.17 16.99 14611 76.00
## [57.065,64.1981) 1988 0.10 10.34 16599 86.35
## [64.1981,71.3312) 1362 0.07 7.08 17961 93.43
## [71.3312,78.4644) 759 0.04 3.95 18720 97.38
## [78.4644,85.5975) 348 0.02 1.81 19068 99.19
## [85.5975,92.7306) 132 0.01 0.69 19200 99.88
## [92.7306,99.8637) 20 0.00 0.10 19220 99.98
## [99.8637,106.997) 3 0.00 0.02 19223 99.99
## [106.997,114.13) 1 0.00 0.01 19224 100.00
Tabla de Frecuenca de la estados State de los datos Coronavirus Confirmados fdt_cat Es la función para determinar frecuencias de datos categóricos
tabla.distribucion <- fdt_cat(datos$State, breaks = "Sturges")
tabla.distribucion
Tabla de Frecuenca de la género (Sex) de los datos Coronavirus Confirmados fdt_cat Es la función para determinar frecuencias de datos categóricos
tabla.distribucion <- fdt_cat(datos$Sex, breaks = "Sturges")
tabla.distribucion
Graficar
Generar gráfica de barra de edades
tabla.distribucion <- fdt(datos$Age, breaks = "Sturges")
tabla.distribucion
## Class limits f rf rf(%) cf cf(%)
## [0,7.13312) 132 0.01 0.69 132 0.69
## [7.13312,14.2662) 118 0.01 0.61 250 1.30
## [14.2662,21.3994) 330 0.02 1.72 580 3.02
## [21.3994,28.5325) 1646 0.09 8.56 2226 11.58
## [28.5325,35.6656) 2798 0.15 14.55 5024 26.13
## [35.6656,42.7987) 3070 0.16 15.97 8094 42.10
## [42.7987,49.9319) 3250 0.17 16.91 11344 59.01
## [49.9319,57.065) 3267 0.17 16.99 14611 76.00
## [57.065,64.1981) 1988 0.10 10.34 16599 86.35
## [64.1981,71.3312) 1362 0.07 7.08 17961 93.43
## [71.3312,78.4644) 759 0.04 3.95 18720 97.38
## [78.4644,85.5975) 348 0.02 1.81 19068 99.19
## [85.5975,92.7306) 132 0.01 0.69 19200 99.88
## [92.7306,99.8637) 20 0.00 0.10 19220 99.98
## [99.8637,106.997) 3 0.00 0.02 19223 99.99
## [106.997,114.13) 1 0.00 0.01 19224 100.00
barplot(height = tabla.distribucion$table$f, names.arg = tabla.distribucion$table$`Class limits`,5)
Generar gráfica de barra para los primeros 5 y últimos 5 etadados con frecuecia de casos
as.data.frame sirve para garatziar que sea un data.frame la tabla de distribucion creada por fdt_cat
tabla.distribucion <- as.data.frame(fdt_cat(datos$State, breaks = "Sturges"))
tabla.distribucion
head(tabla.distribucion, 5)
tail(tabla.distribucion, 5)
barplot(head(tabla.distribucion$f, 5), names.arg = head(tabla.distribucion$Category,5), main = "Casos Confirmados por estado top(5)")
barplot(tail(tabla.distribucion$f, 5), names.arg = tail(tabla.distribucion$Category,5), main = "Casos Confirmados por estado bot(5)")
Generar gráfica de barra para los primeros 5 y últimos 5 etadados con frecuecia de casos
as.data.frame sirve para garatziar que sea un data.frame la tabla de distribucion creada por fdt_cat
tabla.distribucion <- as.data.frame(fdt_cat(datos$Sex, breaks = "Sturges"))
tabla.distribucion
barplot(tabla.distribucion$f, names.arg = tabla.distribucion$Category, main = "Casos Confirmados por Género top(5)")