| Mi primer libro |
data(iris)
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
attach(iris)
table(iris$Species)/length(iris$Species)
##
## setosa versicolor virginica
## 0.3333333 0.3333333 0.3333333
mean(iris$Petal.Width)
## [1] 1.199333
vec<-rnorm(10,20,10)
mean(vec, trim=0.1)
## [1] 23.18979
median(vec)
## [1] 25.51017
moda<-function(var){
frec.var<-table(var)
valor<-which(frec.var==max(frec.var))
names(valor)
}
moda(iris$Species)
## [1] "setosa" "versicolor" "virginica"
quantile(iris$Sepal.Length, seq(0,1,0.01))
## 0% 1% 2% 3% 4% 5% 6% 7% 8% 9% 10% 11% 12%
## 4.300 4.400 4.400 4.547 4.600 4.600 4.694 4.743 4.800 4.800 4.800 4.900 4.900
## 13% 14% 15% 16% 17% 18% 19% 20% 21% 22% 23% 24% 25%
## 4.900 4.900 5.000 5.000 5.000 5.000 5.000 5.000 5.029 5.100 5.100 5.100 5.100
## 26% 27% 28% 29% 30% 31% 32% 33% 34% 35% 36% 37% 38%
## 5.100 5.123 5.200 5.200 5.270 5.400 5.400 5.400 5.400 5.500 5.500 5.500 5.500
## 39% 40% 41% 42% 43% 44% 45% 46% 47% 48% 49% 50% 51%
## 5.511 5.600 5.600 5.600 5.607 5.700 5.700 5.700 5.700 5.700 5.800 5.800 5.800
## 52% 53% 54% 55% 56% 57% 58% 59% 60% 61% 62% 63% 64%
## 5.800 5.800 5.900 5.900 6.000 6.000 6.000 6.000 6.100 6.100 6.100 6.100 6.200
## 65% 66% 67% 68% 69% 70% 71% 72% 73% 74% 75% 76% 77%
## 6.200 6.234 6.300 6.300 6.300 6.300 6.300 6.328 6.400 6.400 6.400 6.400 6.473
## 78% 79% 80% 81% 82% 83% 84% 85% 86% 87% 88% 89% 90%
## 6.500 6.500 6.520 6.600 6.700 6.700 6.700 6.700 6.700 6.763 6.800 6.861 6.900
## 91% 92% 93% 94% 95% 96% 97% 98% 99% 100%
## 6.900 7.008 7.157 7.200 7.255 7.408 7.653 7.700 7.700 7.900
quantile(Sepal.Length, seq(0,1,0.25))
## 0% 25% 50% 75% 100%
## 4.3 5.1 5.8 6.4 7.9
median(iris$Sepal.Length)
## [1] 5.8
tapply(iris$Sepal.Length, iris$Species, summary)
## $setosa
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.300 4.800 5.000 5.006 5.200 5.800
##
## $versicolor
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.900 5.600 5.900 5.936 6.300 7.000
##
## $virginica
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.900 6.225 6.500 6.588 6.900 7.900
max(Sepal.Length)-min(Sepal.Length)
## [1] 3.6
var(iris$Sepal.Length)
## [1] 0.6856935
IQR(iris$Sepal.Length)
## [1] 1.3
Analsis mulrivariado:
Covarianza
cov(iris$Sepal.Length, iris$Sepal.Width)
## [1] -0.042434
cov(iris[,1:4])
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Sepal.Length 0.6856935 -0.0424340 1.2743154 0.5162707
## Sepal.Width -0.0424340 0.1899794 -0.3296564 -0.1216394
## Petal.Length 1.2743154 -0.3296564 3.1162779 1.2956094
## Petal.Width 0.5162707 -0.1216394 1.2956094 0.5810063
cor(iris[,1:4])
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Sepal.Length 1.0000000 -0.1175698 0.8717538 0.8179411
## Sepal.Width -0.1175698 1.0000000 -0.4284401 -0.3661259
## Petal.Length 0.8717538 -0.4284401 1.0000000 0.9628654
## Petal.Width 0.8179411 -0.3661259 0.9628654 1.0000000