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 75 59 23 69 33 33
[31] 31 67 66 33 26 39 37 71 40 71 67 63 78 73 37 29 50 25 66 62 32 51 79 43 32 64 21 50 35 32
[61] 20 57 22 48 44 64 62 44 24 62 77 57 21 44 54 43 69 52 73 26 27 40 75 32 53 24 73 23 38 67
[91] 42 23 71 37 61 20 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 42 80 51 23 58 65
[151] 68 74 72 55 43 67 60 79 48 47 73 21 51 36 36 56 29 70 66 30 70 36 58 32 29 57 74 75 40 29
[181] 78 30 79 66 22 44 63 50 42 70 64 67 23 40 79 80 60 56 71 75 78 22 77 45 73 20 48 33 43 46
[211] 58 37 55 38 44 77 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 48 44 75 76 76 38
[271] 21 68 63 23 30 38 70 70 48 70 60 21 73 39 26 27 33 63 44 55 42 29 70 32 46 54 46 73 46 39
[301] 42 26 47 24 71 46 58 72 41 78 40 44 64 67 45 35 38 51 45 61 55 55 28 47 67 69 20 77 64 67
[331] 61 53 64 25 60 43 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 51 30 78 68 57 44
[391] 47 49 49 31 37 62 26 26 77 25 25 45 55 79 58 29 77 39 22 50 77 66 65 21 34 33 26 49 71 26
[421] 49 53 42 67 59 79 42 53 75 55 63 50 76 29 36 51 54 57 24 73 33 59 38 66 75 72 38 40 41 72
[451] 80 34 43 25 44 32 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 63 63 71 49 38 55
[511] 36 20 58 33 43 71 40 63 38 31 66 28 23 62 70 65 45 22 56 22 65 22 80 72 38 72 72 61 77 49
[541] 70 54 31 76 71 38 41 74 51 22 62 29 62 46 39 38 31 71 57 51 80 77 29 39 45 53 78 31 34 59
[571] 58 34 23 80 35 28 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 40 69 29 37 53 26
[631] 31 51 35 74 74 80 28 62 28 57 68 64 55 49 57 35 78 63 69 43 80 23 65 32 44 26 26 42 43 32
[661] 51 55 28 48 42 57 67 73 54 36 56 57 71 65 39 22 51 59 45 41 51 43 76 36 48 77 44 51 64 68
[691] 72 49 31 47 27 40 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 28 53 68 41 72 54
[751] 80 29 21 21 28 47 74 43 75 50 30 22 53 50 32 37 35 60 28 63 74 52 28 61 66 69 67 54 32 33
[781] 77 60 76 33 61 80 68 80 25 68 37 45 72 68 37 64 23 63 61 58 69 59 38 46 37 30 37 66 71 75
[811] 36 42 77 63 43 45 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 67 55 44 66 67 73
[871] 57 77 43 43 55 57 23 80 44 69 52 22 60 29 26 72 42 80 39 71 80 41 58 49 26 75 54 35 49 70
[901] 52 57 46 50 65 49 73 20 76 65 37 46 79 33 61 23 68 69 75 20 20 60 75 56 72 45 22 79 41 21
[931] 69 76 55 72 43 73 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 64 64 73 29 63 39
[991] 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
Tablas de distribucion 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 distribución del conjunto de datos coronavirus_confirmados.csv Cargar datos de manera local o desde la Web ??
datos <- read.csv("../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 : chr "méxico" "tamaulipas" "distrito federal" "distrito federal" ...
$ Sex : chr "FEMENINO" "MASCULINO" "MASCULINO" "FEMENINO" ...
$ Age : int 75 22 40 29 71 29 61 33 77 84 ...
$ Date : chr "2020-04-09" "2020-04-06" "2020-03-28" "2020-04-01" ...
$ Confirmed: int 1 1 1 1 1 1 1 1 1 1 ...
summary(datos)
X State Sex Age Date
Min. : 0 Length:19224 Length:19224 Min. : 0.00 Length:19224
1st Qu.:22523 Class :character Class :character 1st Qu.: 35.00 Class :character
Median :44009 Mode :character Mode :character Median : 46.00 Mode :character
Mean :44045 Mean : 46.59
3rd Qu.:65793 3rd Qu.: 57.00
Max. :87365 Max. :113.00
Confirmed
Min. :1
1st Qu.:1
Median :1
Mean :1
3rd Qu.:1
Max. :1
datos$State <- toupper(datos$State)
datos
Tabla de frecuencia 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 frecuencia de la estados State de los datos Coronavirus Confirmados fdt_cat Es la función para determinar frecuencias de datos categoricos
tabla.distribucion <- fdt_cat(datos$State, breaks = "Sturges")
tabla.distribucion
Category f rf rf(%) cf cf(%)
DISTRITO FEDERAL 5209 0.27 27.10 5209 27.10
MÉXICO 3130 0.16 16.28 8339 43.38
BAJA CALIFORNIA 1557 0.08 8.10 9896 51.48
TABASCO 984 0.05 5.12 10880 56.60
SINALOA 865 0.04 4.50 11745 61.10
QUINTANA ROO 788 0.04 4.10 12533 65.19
PUEBLA 639 0.03 3.32 13172 68.52
VERACRUZ DE IGNACIO DE LA LLAVE 575 0.03 2.99 13747 71.51
YUCATÁN 448 0.02 2.33 14195 73.84
COAHUILA 407 0.02 2.12 14602 75.96
CHIHUAHUA 351 0.02 1.83 14953 77.78
JALISCO 347 0.02 1.81 15300 79.59
NUEVO LEÓN 334 0.02 1.74 15634 81.33
MORELOS 324 0.02 1.69 15958 83.01
TAMAULIPAS 315 0.02 1.64 16273 84.65
MICHOACÁN 313 0.02 1.63 16586 86.28
BAJA CALIFORNIA SUR 308 0.02 1.60 16894 87.88
GUERRERO 281 0.01 1.46 17175 89.34
GUANAJUATO 263 0.01 1.37 17438 90.71
HIDALGO 261 0.01 1.36 17699 92.07
SONORA 215 0.01 1.12 17914 93.19
AGUASCALIENTES 205 0.01 1.07 18119 94.25
TLAXCALA 193 0.01 1.00 18312 95.26
CHIAPAS 177 0.01 0.92 18489 96.18
OAXACA 138 0.01 0.72 18627 96.89
QUERÉTARO 133 0.01 0.69 18760 97.59
SAN LUIS POTOSÍ 108 0.01 0.56 18868 98.15
CAMPECHE 93 0.00 0.48 18961 98.63
NAYARIT 93 0.00 0.48 19054 99.12
ZACATECAS 77 0.00 0.40 19131 99.52
DURANGO 66 0.00 0.34 19197 99.86
COLIMA 27 0.00 0.14 19224 100.00
Tabla de frecuencia del 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
Category f rf rf(%) cf cf(%)
MASCULINO 11185 0.58 58.18 11185 58.18
FEMENINO 8039 0.42 41.82 19224 100.00
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 estados con frecuencia de casos as.data.frame sirve para garantizar que sea un data.frame la tabla de distribucion creada por fdt_cat
tabla.distribucion <- as.data.frame(fdt_cat(datos$State, breaks = "Sturges"))
There were 20 warnings (use warnings() to see them)
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 last(5)")
Generar gráfica de barra para los primeros 5 y últimos 5 estados con frecuencia de casos as.data.frame sirve para garantizar 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)")