ejemplo_num <- c(175.13, 180.25, 190.14, 160.50, 140.78)
ejemplo_num
## [1] 175.13 180.25 190.14 160.50 140.78
Puedo ver la clase o tipo de dato con la función class():
class(ejemplo_num)
## [1] "numeric"
ejemplo_entero <- c(1L, 3L, 5L, 20L)
class(ejemplo_entero)
## [1] "integer"
ejemplo_nominal <- c("udea", "unal", "eafit", "poli")
class(ejemplo_nominal)
## [1] "character"
ejemplo_ordinal_mal <- factor(c("enero", "febrero", "marzo",
"abril", "mayo", "junio"))
class(ejemplo_ordinal_mal)
## [1] "factor"
Puedo consultar el orden de los niveles con la función levels():
levels(ejemplo_ordinal_mal)
## [1] "abril" "enero" "febrero" "junio" "marzo" "mayo"
Vamos a controlar desde la función factor() el orden de los niveles:
ejemplo_ordinal_bien <- factor(c("enero", "febrero", "marzo",
"abril", "mayo", "junio"),
levels = c("enero", "febrero", "marzo",
"abril", "mayo", "junio"))
levels(ejemplo_ordinal_bien)
## [1] "enero" "febrero" "marzo" "abril" "mayo" "junio"
numeros <- c(2, 3, 4, 5, 8, 2, 1, 50)
mean(numeros)
## [1] 9.375
ejemplo <- c(3, 5, 6, 8, NA)
mean(ejemplo, na.rm = TRUE)
## [1] 5.5
ejemplo2 <- c(1, 30, 40, 51)
mean(ejemplo2)
## [1] 30.5
sum(ejemplo2) / length(ejemplo2)
## [1] 30.5
names(women)
## [1] "height" "weight"
class(women$height)
## [1] "numeric"
mean(women$height)
## [1] 65
length(women$height)
## [1] 15
animales <- c(2, 30, 40, 60)
peso <- c(30, 43, 51, 60)
sum(animales * peso) / sum(animales)
## [1] 52.95455
mean(peso)
## [1] 46
weighted.mean(peso, animales)
## [1] 52.95455
median(women$height)
## [1] 65
moda <- function(x) {
ux = unique(x)
tab = tabulate(match(x, ux))
ux[tab == max(tab)]
}
letras <- c("a", "a", "b", "c", "d")
moda(letras)
## [1] "a"
numeros <- c(1, 2, 3, 3, 3, 1, 5)
moda(numeros)
## [1] 3
numeros2 <- c(1, 1, 2, 2, 3)
moda(numeros2)
## [1] 1 2
altura_ordenada <- sort(women$height, decreasing = FALSE)
altura_ordenada
## [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
median(altura_ordenada)
## [1] 65
\[P_k = \frac{k(n+1)}{100}\]
# Ubicación del percentil 50
(50 * (15 + 1)) / 100
## [1] 8
altura_ordenada[8]
## [1] 65
quantile(women$height, probs = 0.50)
## 50%
## 65
quantile(women$height, probs = seq(0, 1, 0.01))
## 0% 1% 2% 3% 4% 5% 6% 7% 8% 9% 10% 11% 12%
## 58.00 58.14 58.28 58.42 58.56 58.70 58.84 58.98 59.12 59.26 59.40 59.54 59.68
## 13% 14% 15% 16% 17% 18% 19% 20% 21% 22% 23% 24% 25%
## 59.82 59.96 60.10 60.24 60.38 60.52 60.66 60.80 60.94 61.08 61.22 61.36 61.50
## 26% 27% 28% 29% 30% 31% 32% 33% 34% 35% 36% 37% 38%
## 61.64 61.78 61.92 62.06 62.20 62.34 62.48 62.62 62.76 62.90 63.04 63.18 63.32
## 39% 40% 41% 42% 43% 44% 45% 46% 47% 48% 49% 50% 51%
## 63.46 63.60 63.74 63.88 64.02 64.16 64.30 64.44 64.58 64.72 64.86 65.00 65.14
## 52% 53% 54% 55% 56% 57% 58% 59% 60% 61% 62% 63% 64%
## 65.28 65.42 65.56 65.70 65.84 65.98 66.12 66.26 66.40 66.54 66.68 66.82 66.96
## 65% 66% 67% 68% 69% 70% 71% 72% 73% 74% 75% 76% 77%
## 67.10 67.24 67.38 67.52 67.66 67.80 67.94 68.08 68.22 68.36 68.50 68.64 68.78
## 78% 79% 80% 81% 82% 83% 84% 85% 86% 87% 88% 89% 90%
## 68.92 69.06 69.20 69.34 69.48 69.62 69.76 69.90 70.04 70.18 70.32 70.46 70.60
## 91% 92% 93% 94% 95% 96% 97% 98% 99% 100%
## 70.74 70.88 71.02 71.16 71.30 71.44 71.58 71.72 71.86 72.00
quantile(women$height, probs = seq(0, 1, 0.1))
## 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
## 58.0 59.4 60.8 62.2 63.6 65.0 66.4 67.8 69.2 70.6 72.0
quantile(women$height, probs = 0.80)
## 80%
## 69.2
quantile(women$height, probs = seq(0, 1, 0.25))
## 0% 25% 50% 75% 100%
## 58.0 61.5 65.0 68.5 72.0
var(women$height)
## [1] 20
sd(women$height)
## [1] 4.472136
sd(women$weight)
## [1] 15.49869
media_altura <- mean(women$height)
media_peso <- mean(women$weight)
desv_altura <- sd(women$height)
desv_peso <- sd(women$weight)
(desv_altura / media_altura) * 100
## [1] 6.880209
(desv_peso / media_peso) * 100
## [1] 11.33498
range(women$height)
## [1] 58 72
min(women$height)
## [1] 58
max(women$height)
## [1] 72
IQR(women$height)
## [1] 7
library(moments)
skewness(women$height)
## [1] 0
kurtosis(women$height)
## [1] 1.789286