Laporan Praktikum 01
Algoritma dan Pemrograman dalam Bahasa R
Dasar R dan Operasi Aritmetika
Mendefenisikan sebuah objek dalam bahasa R bisa menggunakan <-atau =.
## [1] 11
## [1] 11
## [1] 196
## [1] 0.5714286
## [1] 3
## [1] 6 14 36 10
## [1] 1 2 4 5 6 7 9 2
Objek Data
Sedikitnya terdapat 6 objek data yang sering digunakan da;am bahasa R yaitu vektor, matriks, faktor, array, list dan data frame. ### 1. VEKTOR DAN MENGAKSESNYA
## [1] 2 3 4 5
## [1] "sd" "smp" "sma"
## [1] "sd"
## [1] "sd" "smp" "sma"
## [1] "sd" "sma"
## [1] NA
## fisika
## 95
#named chr
profil <- c(nama = "Budi", tempat_tinggal = "Jakarta", tingkat_pendidikan = "S1")
#Tampilkan variable profil
print(profil)## nama tempat_tinggal tingkat_pendidikan
## "Budi" "Jakarta" "S1"
2. MATRIKS DAN MENGAKSESNYA
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [1] 1
## [1] 2 4
3. FACTOR DAN MENGAKSESNYA
Factor adalah tipe khusus vektor dengan elemen data kategori, vektor yang mengindikasikan taraf(level) dari peubah kategori.
## [1] "sd" "smp" "sma"
## [1] sd smp sma
## Levels: sd sma smp
## [1] sd smp sma
## Levels: sd smp sma
4. ARRAY DAN MENGAKSESNYA
a14 <-1:12
c1<-array(a14,dim=c(2,2,3))
c2<-array(a14,dim=c(2,1,2,3))
c3 <-array(a14,dim=c(1,2,4,2))
c4 <-array(a14,dim=c(3,4))
#akses array
c2[,,1,] #lembar 1 dari c2## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
## [1] 9 10
5. LIST DAN MENGAKSESNYA
List adalah objek data yang elemen didalamnya boleh memiliki mode atau objek yang berbeda.
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
##
## [[2]]
## [1] 1 2 3
##
## [[3]]
## [1] "a" "b"
##
## $A
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## [1] "a" "b"
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
6. DATA FRAME DAN MENGAKSESNYA
Mirip Tabel - Menyimpan data berbagai bentuk dalam bentuk matriks atau tabel.
#Membuat dua variable vector
fakultas <- c("Bisnis", "D3 Perhotelan", "ICT", "Ilmu Komunikasi", "Seni dan Desain")
jumlah_mahasiswa <- c(260, 28, 284, 465, 735)
#Membuat data frame dari kedua vector di atas
info_mahasiswa <- data.frame(fakultas, jumlah_mahasiswa)
#Melihat isi data frame
info_mahasiswa## fakultas jumlah_mahasiswa
## 1 Bisnis 260
## 2 D3 Perhotelan 28
## 3 ICT 284
## 4 Ilmu Komunikasi 465
## 5 Seni dan Desain 735
#Buat vector baru sebagai representasi akreditasi
akreditasi <- c("A","A","B","A","A")
#Buat data frame dari ketiga vector di atas
info_mahasiswa <- data.frame(fakultas, jumlah_mahasiswa, akreditasi)
info_mahasiswa## fakultas jumlah_mahasiswa akreditasi
## 1 Bisnis 260 A
## 2 D3 Perhotelan 28 A
## 3 ICT 284 B
## 4 Ilmu Komunikasi 465 A
## 5 Seni dan Desain 735 A
## [1] 260 28 284 465 735
## [1] "Bisnis" "D3 Perhotelan" "ICT" "Ilmu Komunikasi"
## [5] "Seni dan Desain"
Import File dan Mengakses variabel
Dalam melakukan import data dalam suatu file. misalnya csv dapat menggunakan fungsi read.csv() atau file xlsx dengan fungsi read_excel() yang terdapat pada package readxl.
## Warning: package 'readxl' was built under R version 4.0.4
## tibble[,9] [397 x 9] (S3: tbl_df/tbl/data.frame)
## $ mpg : num [1:397] 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : num [1:397] 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num [1:397] 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : num [1:397] 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : num [1:397] 3504 3693 3436 3433 3449 ...
## $ acceleration: num [1:397] 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : num [1:397] 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : num [1:397] 1 1 1 1 1 1 1 1 1 1 ...
## $ name : chr [1:397] "chevrolet chevelle malibu" "buick skylark 320" "plymouth satellite" "amc rebel sst" ...
# MEMBUAT VARIABEL BARU DARI KOMBINASI VARIABEL YANG ADA
a = auto$mpg*auto$cylinders
auto = cbind(auto,a)
head(auto)## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name a
## 1 chevrolet chevelle malibu 144
## 2 buick skylark 320 120
## 3 plymouth satellite 144
## 4 amc rebel sst 128
## 5 ford torino 136
## 6 ford galaxie 500 120
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name
## 1 chevrolet chevelle malibu
## 2 buick skylark 320
## 3 plymouth satellite
## 4 amc rebel sst
## 5 ford torino
## 6 ford galaxie 500
## name mpg cylinders displacement horsepower weight
## 1 chevrolet chevelle malibu 18 8 307 130 3504
## 2 buick skylark 320 15 8 350 165 3693
## 3 plymouth satellite 18 8 318 150 3436
## 4 amc rebel sst 16 8 304 150 3433
## 5 ford torino 17 8 302 140 3449
## 6 ford galaxie 500 15 8 429 198 4341
## acceleration year origin
## 1 12.0 70 1
## 2 11.5 70 1
## 3 11.0 70 1
## 4 12.0 70 1
## 5 10.5 70 1
## 6 10.0 70 1
## [1] 2.890372 2.708050 2.890372 2.772589 2.833213 2.708050
## name mpg cylinders displacement horsepower weight
## 1 chevrolet chevelle malibu 18 8 307 130 3504
## 2 buick skylark 320 15 8 350 165 3693
## 3 plymouth satellite 18 8 318 150 3436
## 4 amc rebel sst 16 8 304 150 3433
## 5 ford torino 17 8 302 140 3449
## 6 ford galaxie 500 15 8 429 198 4341
## acceleration year origin lnmpg
## 1 12.0 70 1 2.890372
## 2 11.5 70 1 2.708050
## 3 11.0 70 1 2.890372
## 4 12.0 70 1 2.772589
## 5 10.5 70 1 2.833213
## 6 10.0 70 1 2.708050
# RENAME VARIABEL (KOMBINASI MEMBUAT DAN MENGHAPUS VARIABEL)
asal = auto$origin
auto = cbind(auto,asal)
head(auto)## name mpg cylinders displacement horsepower weight
## 1 chevrolet chevelle malibu 18 8 307 130 3504
## 2 buick skylark 320 15 8 350 165 3693
## 3 plymouth satellite 18 8 318 150 3436
## 4 amc rebel sst 16 8 304 150 3433
## 5 ford torino 17 8 302 140 3449
## 6 ford galaxie 500 15 8 429 198 4341
## acceleration year origin lnmpg asal
## 1 12.0 70 1 2.890372 1
## 2 11.5 70 1 2.708050 1
## 3 11.0 70 1 2.890372 1
## 4 12.0 70 1 2.772589 1
## 5 10.5 70 1 2.833213 1
## 6 10.0 70 1 2.708050 1
## name mpg cylinders displacement horsepower weight
## 1 chevrolet chevelle malibu 18 8 307 130 3504
## 2 buick skylark 320 15 8 350 165 3693
## 3 plymouth satellite 18 8 318 150 3436
## 4 amc rebel sst 16 8 304 150 3433
## 5 ford torino 17 8 302 140 3449
## 6 ford galaxie 500 15 8 429 198 4341
## acceleration year lnmpg asal
## 1 12.0 70 2.890372 1
## 2 11.5 70 2.708050 1
## 3 11.0 70 2.890372 1
## 4 12.0 70 2.772589 1
## 5 10.5 70 2.833213 1
## 6 10.0 70 2.708050 1
## name mpg cylinders displacement horsepower weight
## 1 chevrolet chevelle malibu 18 8 307 130 3504
## 2 buick skylark 320 15 8 350 165 3693
## 3 plymouth satellite 18 8 318 150 3436
## 4 amc rebel sst 16 8 304 150 3433
## 5 ford torino 17 8 302 140 3449
## 6 ford galaxie 500 15 8 429 198 4341
## acceleration year lnmpg asal
## 1 12.0 70 2.890372 1
## 2 11.5 70 2.708050 1
## 3 11.0 70 2.890372 1
## 4 12.0 70 2.772589 1
## 5 10.5 70 2.833213 1
## 6 10.0 70 2.708050 1
Struktur Kendali
Eksekusi bersyarat
•if (kondisi) ekspresi else ekspresi
•if else(kondisi,ekspresi benar,ekspresi salah)
•switch(“kondisi”=ekspresi,…)
Pengulangan(loops)
•for (objekin sekuens) ekspresi
•while (kondisi) ekspresi
•repeat ekspresi (untuk menghentikan gunakan perintah break)
Tanpa pengulangan
•apply(array, margin, function, function args)
1. While Loop
## [1] "Hello"
## [1] "Hello"
## [1] "Hello"
## [1] "Hello"
## [1] "Hello"
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
2. For Loop
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
## 1
## 12
## 123
## 1234
## 12345
## 123456
## 1234567
## 12345678
## 123456789
## 12345678910
#menggunakan BREAK
a<- 0
while (a<10) {
print(paste('Nilai a saat ini adalah = ' ,a))
if (a==7)
{
break
}
a <- a+1
}## [1] "Nilai a saat ini adalah = 0"
## [1] "Nilai a saat ini adalah = 1"
## [1] "Nilai a saat ini adalah = 2"
## [1] "Nilai a saat ini adalah = 3"
## [1] "Nilai a saat ini adalah = 4"
## [1] "Nilai a saat ini adalah = 5"
## [1] "Nilai a saat ini adalah = 6"
## [1] "Nilai a saat ini adalah = 7"
## [1] 1
## [1] 2
## [1] 3
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
Statistika Deskriptif
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 9.00 17.50 23.00 23.52 29.00 46.60
## [1] 23.51587
hist(auto$mpg, main="Histogram mpg", col="yellow", xlab="mpg mobil")
# MENYANDINGKAN 2 GRAFIK
par(mfrow = c(1,2))
hist(auto$mpg, main="Histogram mpg", col="yellow", xlab="mpg mobil")
hist(auto$cylinders, main="Histogram silinder", col="green", xlab="banyaknya silinder")# MENYANDINGKAN 3 GRAFIK
par(mfrow = c(1,3))
boxplot(auto$mpg, main="Boxplot mpg")
hist(auto$mpg, main="Histogram mpg", col="yellow", xlab="mpg mobil")
hist(auto$cylinders, main="Histogram silinder", col="green", xlab="banyaknya silinder")# MEMBUAT PLOT (SCATTERPLOT X vs Y)
par(mfrow = c(1,1))
plot(auto$mpg, auto$displacement, xlab="mpg mobil", ylab="displacement")## mpg cylinders displacement horsepower weight
## mpg 1.0000000 -0.7762599 -0.8044430 NA -0.8317389
## cylinders -0.7762599 1.0000000 0.9509199 NA 0.8970169
## displacement -0.8044430 0.9509199 1.0000000 NA 0.9331044
## horsepower NA NA NA 1 NA
## weight -0.8317389 0.8970169 0.9331044 NA 1.0000000
## acceleration 0.4222974 -0.5040606 -0.5441618 NA -0.4195023
## year 0.5814695 -0.3467172 -0.3698041 NA -0.3079004
## lnmpg 0.9838107 -0.8263150 -0.8537047 NA -0.8756042
## asal 0.5636979 -0.5649716 -0.6106643 NA -0.5812652
## acceleration year lnmpg asal
## mpg 0.4222974 0.5814695 0.9838107 0.5636979
## cylinders -0.5040606 -0.3467172 -0.8263150 -0.5649716
## displacement -0.5441618 -0.3698041 -0.8537047 -0.6106643
## horsepower NA NA NA NA
## weight -0.4195023 -0.3079004 -0.8756042 -0.5812652
## acceleration 1.0000000 0.2829009 0.4471706 0.2100836
## year 0.2829009 1.0000000 0.5778566 0.1843141
## lnmpg 0.4471706 0.5778566 1.0000000 0.5588776
## asal 0.2100836 0.1843141 0.5588776 1.0000000
## mpg cylinders displacement horsepower weight acceleration
## mpg 1.000 -0.778 -0.805 -0.778 -0.832 0.423
## cylinders -0.778 1.000 0.951 0.843 0.898 -0.505
## displacement -0.805 0.951 1.000 0.897 0.933 -0.544
## horsepower -0.778 0.843 0.897 1.000 0.865 -0.689
## weight -0.832 0.898 0.933 0.865 1.000 -0.417
## acceleration 0.423 -0.505 -0.544 -0.689 -0.417 1.000
## year 0.581 -0.346 -0.370 -0.416 -0.309 0.290
## lnmpg 0.984 -0.827 -0.854 -0.830 -0.876 0.448
## asal 0.565 -0.569 -0.615 -0.455 -0.585 0.213
## year lnmpg asal
## mpg 0.581 0.984 0.565
## cylinders -0.346 -0.827 -0.569
## displacement -0.370 -0.854 -0.615
## horsepower -0.416 -0.830 -0.455
## weight -0.309 -0.876 -0.585
## acceleration 0.290 0.448 0.213
## year 1.000 0.577 0.182
## lnmpg 0.577 1.000 0.561
## asal 0.182 0.561 1.000
##
## 1 2 3
## 3 0 0 4
## 4 69 61 69
## 5 0 3 0
## 6 73 4 6
## 8 103 0 0
## Analysis of Variance Table
##
## Response: auto$mpg
## Df Sum Sq Mean Sq F value Pr(>F)
## factor(auto$asal) 2 8081.1 4040.5 98.445 < 2.2e-16 ***
## Residuals 394 16171.2 41.0
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## cylinders asal
## cylinders 1.0000000 -0.6071163
## asal -0.6071163 1.0000000
## cylinders asal
## cylinders 1.0000000 -0.5649716
## asal -0.5649716 1.0000000
Manajemen Data Frame (Data Wrangling/Munging)
Membuat Peubah Baru
Perl <-paste("P",rep(1:4,each=3),sep="")
Kel<-factor(rep(1:3,4))
Resp<-seq(1,23,by=2)
data1 <-data.frame(Perl,Kel,Resp)
head(data1)## Perl Kel Resp
## 1 P1 1 1
## 2 P1 2 3
## 3 P1 3 5
## 4 P2 1 7
## 5 P2 2 9
## 6 P2 3 11
## Perl Kel Resp baru1
## 1 P1 1 1 1
## 2 P1 2 3 2
## 3 P1 3 5 3
## 4 P2 1 7 4
## 5 P2 2 9 5
## 6 P2 3 11 6
Subsetting Data
•Dilakukan untuk akses sebagian data
•Membuat ide logic untuk diterapkan dalam vektor logic yang diinginkan
•Fungsi yang digunakan : ==, !=, >, >=, <, <=, %in%, duplicated(…),which(…),is.na(…), is.null(…), is.numeric(…), dll…
## Perl Kel Resp baru1
## 1 P1 1 1 1
## 4 P2 1 7 4
## 7 P3 1 13 7
## 10 P4 1 19 10
#Subset kelompok 1 atau perlakuan P2
indeks2 <-data1$Kel == 1 | data1$Perl == "P2"
data3 <-data1[indeks2,]
head(data3)## Perl Kel Resp baru1
## 1 P1 1 1 1
## 4 P2 1 7 4
## 5 P2 2 9 5
## 6 P2 3 11 6
## 7 P3 1 13 7
## 10 P4 1 19 10
#Subset respon bilangan prima
indeks3 <-data1$Resp %in% c(2,3,5,7,11,13,17,19,23)
data4 <-data1[indeks3,]
head(data4)## Perl Kel Resp baru1
## 2 P1 2 3 2
## 3 P1 3 5 3
## 4 P2 1 7 4
## 6 P2 3 11 6
## 7 P3 1 13 7
## 9 P3 3 17 9
Sorting Data
•Dilakukan untuk mengurutkan data berdasarkan beberapa peubah tertentu
•Dilakukan dengan membuat vektor logika untuk melakukan pengurutan data
•Fungsi yang sering digunakan order(), sort(), rev(), unique()
#Sorting berdasar kelompok secara ascending
indeks4 <-order(data1$Kel)
data5 <-data1[indeks4,]
#Sorting berdasar kelompok secara descending
indeks5 <-order(data1$Kel, data1$Resp, decreasing=TRUE)
data6 <-data1[indeks5,]
head(data6)## Perl Kel Resp baru1
## 12 P4 3 23 12
## 9 P3 3 17 9
## 6 P2 3 11 6
## 3 P1 3 5 3
## 11 P4 2 21 11
## 8 P3 2 15 8
#Sorting berdasarkan kelompok secara ascending dan respon secara descending
indeks6 <-order(data1$Resp, decreasing=TRUE)
data7 <-data1[indeks6,]
head(data7)## Perl Kel Resp baru1
## 12 P4 3 23 12
## 11 P4 2 21 11
## 10 P4 1 19 10
## 9 P3 3 17 9
## 8 P3 2 15 8
## 7 P3 1 13 7
## Perl Kel Resp baru1
## 10 P4 1 19 10
## 7 P3 1 13 7
## 4 P2 1 7 4
## 1 P1 1 1 1
## 11 P4 2 21 11
## 8 P3 2 15 8
## [1] 19 13 7 1 21 15 9 3 23 17 11 5
## [1] 1 3 5 7 9 11 13 15 17 19 21 23
## [1] 5 11 17 23 3 9 15 21 1 7 13 19
## [1] 4 8 12 3 7 11 2 6 10 1 5 9
## [1] 10 7 4 1 11 8 5 2 12 9 6 3
## [1] TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE
## [1] 1 2 5 6 9 10 11
## [1] 19 13 21 15 23 17 11
## [1] 19 13 21 15 23 17 11
Recoding Data
•Digunakan untuk membuat nilai baru dari nilai peubah yang sudah ada
•Dapat dilakukan secara logical, fungsi ifelse(), dan fungsi recode()
## Perl Kel Resp baru1 Code1
## 10 P4 1 19 10 0
## 7 P3 1 13 7 1
## 4 P2 1 7 4 1
## 1 P1 1 1 1 1
## 11 P4 2 21 11 0
## 8 P3 2 15 8 0
## Perl Kel Resp baru1 Code1 Code2
## 10 P4 1 19 10 0 0
## 7 P3 1 13 7 1 1
## 4 P2 1 7 4 1 1
## 1 P1 1 1 1 1 1
## 11 P4 2 21 11 0 0
## 8 P3 2 15 8 0 0
## Loading required package: carData
## Perl Kel Resp baru1 Code1 Code2 Code3
## 10 P4 1 19 10 0 0 0
## 7 P3 1 13 7 1 1 1
## 4 P2 1 7 4 1 1 1
## 1 P1 1 1 1 1 1 1
## 11 P4 2 21 11 0 0 0
## 8 P3 2 15 8 0 0 0
Merging Data
•Bisa dilakukan dengan rbind() atau cbind()
•Lebih mudah dengan fungsi merge()
# Gabung data 1 dan tabel 1
tabel1 <- data.frame(Tr=c("P4","P2","P5"), k1=c(50,100,200))
data9 <- merge(data1, tabel1, by.x=1, by.y=1, all=FALSE)
head(data9)## Perl Kel Resp baru1 k1
## 1 P2 3 11 6 100
## 2 P2 1 7 4 100
## 3 P2 2 9 5 100
## 4 P4 1 19 10 50
## 5 P4 2 21 11 50
## 6 P4 3 23 12 50
## Perl Kel Resp baru1 k1
## 1 P1 1 1 1 NA
## 2 P1 2 3 2 NA
## 3 P1 3 5 3 NA
## 4 P2 3 11 6 100
## 5 P2 1 7 4 100
## 6 P2 2 9 5 100
Reshaping Data
Membentuk data baru dengan cara :
•Long to wide format
•Wide to longformat
•Menggunakan fungsi reshape()
#long to wide
data11 <-reshape(data1[,-4], idvar="Perl", timevar="Kel", direction="wide")
head(data11)## Perl Resp.1 Resp.2 Resp.3
## 1 P1 1 3 5
## 4 P2 7 9 11
## 7 P3 13 15 17
## 10 P4 19 21 23
## Perl Kel Resp.1
## P1.1 P1 1 1
## P2.1 P2 1 7
## P3.1 P3 1 13
## P4.1 P4 1 19
## P1.2 P1 2 3
## P2.2 P2 2 9