Objectifs:
##
## Attachement du package : 'dplyr'
## Les objets suivants sont masqués depuis 'package:stats':
##
## filter, lag
## Les objets suivants sont masqués depuis 'package:base':
##
## intersect, setdiff, setequal, union
dim(df)
## [1] 150 5
Iris comporte donc 150 lignes et 5 colonnes
summary(df)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
La dernière colonne nous donne le nombre de chaque espèce:
Iris, utilisation de la library skimrOn peut ainsi examiner le nombre d’échantillons, leur type (numérique vs catégoriel), les moyennes et sd de chaque groupe.
library(skimr)
group_by(df, Species) %>% skim()
| Name | Piped data |
| Number of rows | 150 |
| Number of columns | 5 |
| _______________________ | |
| Column type frequency: | |
| numeric | 4 |
| ________________________ | |
| Group variables | Species |
Variable type: numeric
| skim_variable | Species | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Sepal.Length | setosa | 0 | 1 | 5.01 | 0.35 | 4.3 | 4.80 | 5.00 | 5.20 | 5.8 | ▃▃▇▅▁ |
| Sepal.Length | versicolor | 0 | 1 | 5.94 | 0.52 | 4.9 | 5.60 | 5.90 | 6.30 | 7.0 | ▂▇▆▃▃ |
| Sepal.Length | virginica | 0 | 1 | 6.59 | 0.64 | 4.9 | 6.23 | 6.50 | 6.90 | 7.9 | ▁▃▇▃▂ |
| Sepal.Width | setosa | 0 | 1 | 3.43 | 0.38 | 2.3 | 3.20 | 3.40 | 3.68 | 4.4 | ▁▃▇▅▂ |
| Sepal.Width | versicolor | 0 | 1 | 2.77 | 0.31 | 2.0 | 2.52 | 2.80 | 3.00 | 3.4 | ▁▅▆▇▂ |
| Sepal.Width | virginica | 0 | 1 | 2.97 | 0.32 | 2.2 | 2.80 | 3.00 | 3.18 | 3.8 | ▂▆▇▅▁ |
| Petal.Length | setosa | 0 | 1 | 1.46 | 0.17 | 1.0 | 1.40 | 1.50 | 1.58 | 1.9 | ▁▃▇▃▁ |
| Petal.Length | versicolor | 0 | 1 | 4.26 | 0.47 | 3.0 | 4.00 | 4.35 | 4.60 | 5.1 | ▂▂▇▇▆ |
| Petal.Length | virginica | 0 | 1 | 5.55 | 0.55 | 4.5 | 5.10 | 5.55 | 5.88 | 6.9 | ▃▇▇▃▂ |
| Petal.Width | setosa | 0 | 1 | 0.25 | 0.11 | 0.1 | 0.20 | 0.20 | 0.30 | 0.6 | ▇▂▂▁▁ |
| Petal.Width | versicolor | 0 | 1 | 1.33 | 0.20 | 1.0 | 1.20 | 1.30 | 1.50 | 1.8 | ▅▇▃▆▁ |
| Petal.Width | virginica | 0 | 1 | 2.03 | 0.27 | 1.4 | 1.80 | 2.00 | 2.30 | 2.5 | ▂▇▆▅▇ |
seto <- df[df$Species=='setosa',]
summary(seto)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.300 Min. :1.000 Min. :0.100
## 1st Qu.:4.800 1st Qu.:3.200 1st Qu.:1.400 1st Qu.:0.200
## Median :5.000 Median :3.400 Median :1.500 Median :0.200
## Mean :5.006 Mean :3.428 Mean :1.462 Mean :0.246
## 3rd Qu.:5.200 3rd Qu.:3.675 3rd Qu.:1.575 3rd Qu.:0.300
## Max. :5.800 Max. :4.400 Max. :1.900 Max. :0.600
## Species
## setosa :50
## versicolor: 0
## virginica : 0
##
##
##
La valeur minimale pour l’espèce setos pour Sepal.Length est 4.3
Dans le tableau ci-dessus, la moyenne est dans la colonne mean et l’écart-type dans la colonne sd
df <- df %>% mutate(Sepet.Length= Petal.Length+Petal.Width+ Sepal.Length + Sepal.Width)
head(df)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepet.Length
## 1 5.1 3.5 1.4 0.2 setosa 10.2
## 2 4.9 3.0 1.4 0.2 setosa 9.5
## 3 4.7 3.2 1.3 0.2 setosa 9.4
## 4 4.6 3.1 1.5 0.2 setosa 9.4
## 5 5.0 3.6 1.4 0.2 setosa 10.2
## 6 5.4 3.9 1.7 0.4 setosa 11.4
df2<-data.frame(df)
df3 <- rbind(df, df2)
dim(df3)
## [1] 300 6
On confirme que le nombre de ligne a doublé mais que le nombre de colonne est bien le même (les 5 colonnes initiales et la colonne qu’on a ajoutée)
condition <- df[df$Sepal.Width>3,]
condition2 <- condition[df$Sepal.Length<5, ]
mean(condition2$Sepal.Width, na.rm=TRUE)
## [1] 3.452381
La moyenne de Sepal.Width conditionnée par Sepal.width > 3 et Sepal.Length < 5 est 3.45
Var.df<-df[ , c(1:4)] # df sans la variable catégorielle "espèce"
cormatrix <- cor.test(df$Sepal.Length, df$Petal.Length)
cormatrix
##
## Pearson's product-moment correlation
##
## data: df$Sepal.Length and df$Petal.Length
## t = 21.646, df = 148, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.8270363 0.9055080
## sample estimates:
## cor
## 0.8717538
le coefficient de corrélation netre taille de sépale et de pétale est de 0.87, donc les 2 variables sont corrélées le p-value est < 2.2e-16
plt <- ggplot(data=df, aes(x = Sepal.Length, y = Petal.Length))
plt<- plt + geom_point(aes(color=Species, shape=Species)) +
xlab("Sepal Length") + ylab("Petal Length") +
ggtitle("Sepal vs Petal length") +
geom_smooth(method="lm")
plt
## `geom_smooth()` using formula 'y ~ x'