Data

iris=read.csv("D:/iris.csv",header=TRUE)
head(iris)
##   Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm     Species
## 1  1           5.1          3.5           1.4          0.2 Iris-setosa
## 2  2           4.9          3.0           1.4          0.2 Iris-setosa
## 3  3           4.7          3.2           1.3          0.2 Iris-setosa
## 4  4           4.6          3.1           1.5          0.2 Iris-setosa
## 5  5           5.0          3.6           1.4          0.2 Iris-setosa
## 6  6           5.4          3.9           1.7          0.4 Iris-setosa

Menghitung Statistik

summary(iris)
##        Id         SepalLengthCm    SepalWidthCm   PetalLengthCm  
##  Min.   :  1.00   Min.   :4.300   Min.   :2.000   Min.   :1.000  
##  1st Qu.: 38.25   1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600  
##  Median : 75.50   Median :5.800   Median :3.000   Median :4.350  
##  Mean   : 75.50   Mean   :5.843   Mean   :3.054   Mean   :3.759  
##  3rd Qu.:112.75   3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100  
##  Max.   :150.00   Max.   :7.900   Max.   :4.400   Max.   :6.900  
##   PetalWidthCm     Species         
##  Min.   :0.100   Length:150        
##  1st Qu.:0.300   Class :character  
##  Median :1.300   Mode  :character  
##  Mean   :1.199                     
##  3rd Qu.:1.800                     
##  Max.   :2.500

#Modus

modus <- function(x){
  uniqx <- unique (x)
  uniqx[which.max(
tabulate(match(x,
uniqx)))] 
}
modus(iris$SepalLengthCm)
## [1] 5
modus(iris$SepalWidthCm)
## [1] 3
modus(iris$PetalLengthCm)
## [1] 1.5
modus(iris$PetalWidthCm)
## [1] 0.2

#Rata-rata Geometri

mean.geom <- function(x){
  exp(mean(log(x)))
}
mean.geom(iris$SepalLengthCm)
## [1] 5.78572
mean.geom(iris$SepalWidthCm)
## [1] 3.023582
mean.geom(iris$PetalLengthCm)
## [1] 3.239757
mean.geom(iris$PetalLengthCm)
## [1] 3.239757

#Rata-rata Harmoni

mean.harm <- function(x){
  1/(mean(1/x))
}
mean.harm(iris$SepalLengthCm)
## [1] 5.728905
mean.harm(iris$SepalWidthCm)
## [1] 2.993137
mean.harm(iris$PetalLengthCm)
## [1] 2.696472
mean.harm(iris$PetalLengthCm)
## [1] 2.696472

#Nilai Skewness dan kurtosis

library(e1071)
skewness(iris$SepalLengthCm)
## [1] 0.3086407
skewness(iris$SepalWidthCm)
## [1] 0.3274013
skewness(iris$PetalLengthCm)
## [1] -0.2689994
skewness(iris$PetalWidthCm)
## [1] -0.102906
kurtosis(iris$SepalLengthCm)
## [1] -0.6058125
kurtosis(iris$SepalWidthCm)
## [1] 0.1983681
kurtosis(iris$PetalLengthCm)
## [1] -1.416683
kurtosis(iris$PetalWidthCm)
## [1] -1.357368

Menampilkan Grafik

Diagram Kotak Garis

boxplot(iris$SepalLengthCm, main = 
"SepalLengthCm", xlab = "SepalLength (cm)", col = "red" )

boxplot(iris$SepalWidthCm, main = 
"SepalWidthCm", xlab = "SepalWidth (cm)", col =  "orange")

boxplot(iris$PetalLengthCm, main = 
"petalLengthCm", xlab = "PetalLength (cm)", col = "yellow")

boxplot(iris$PetalWidthCm, main = 
"petalWidthCm", xlab = "PetalWidth (cm)", col = "green")

Histogram

hist(iris$SepalLengthCm, main = "SepalLengthCm", xlab ="SepalLength (Cm)", col = "magenta")

hist(iris$SepalWidthCm, main = "SepalWidthCm", xlab ="SepalWidth (Cm)", col = "chocolate")

hist(iris$PetalLengthCm, main = "PetalLengthCm", xlab ="PetalLength (Cm)", col = "Pink")

hist(iris$PetalWidthCm, main = "PetalWIdthCm", xlab ="PetalWidth (Cm)", col = "Darkgrey")

Diagram Pencar

par(mfrow=c(2,2))
plot(iris$SepalLengthCm, main = "SepalLengthCm", xlab ="SepalLength (Cm)", col = "magenta")

plot(iris$SepalWidthCm, main = "SepalWidthCm", xlab ="SepalWidth (Cm)", col = "chocolate")

plot(iris$PetalLengthCm, main = "PetalLengthCm", xlab ="PetalLength (Cm)", col = "Pink")

plot(iris$PetalWidthCm, main = "PetalWIdthCm", xlab ="PetalWidth (Cm)", col = "Darkgrey")

Diagram Batang

barplot(iris$SepalLengthCm, main = "SepalLengthCm", xlab ="SepalLength (Cm)", col = "magenta")

barplot(iris$SepalWidthCm, main = "SepalWidthCm", xlab ="SepalWidth (Cm)", col = "chocolate")

barplot(iris$PetalLengthCm, main = "PetalLengthCm", xlab ="PetalLength (Cm)", col = "Pink")

barplot(iris$PetalWidthCm, main = "PetalWIdthCm", xlab ="PetalWidth (Cm)", col = "Darkgrey")

#membuat pie chart untuk spesies
pie(table(iris$Species))