Laporan 1 Praktikum Pemrograman Statatitistik - Tugas Satria June Adwendi
Praktikum Pertemuan 1
Instalasi Linux
source: https://docs.microsoft.com/en-us/windows/wsl/install-win10
Tutorial: https://youtu.be/47ksZLEDLFE
Install the Windows Subsystem for Linux
Before installing any Linux distros for WSL, you must ensure that the “Windows Subsystem for Linux” optional feature is enabled:
Open PowerShell as Administrator and run: Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Restart your computer when prompted.
Install your Linux Distribution of Choice
To download and install your preferred distro(s), you have three choices:
Download and install from the Windows Store (see below)
Download and install from the Command-Line/Script (read the manual installation instructions)
Download and manually unpack and install (for Windows Server – instructions here)
Windows 10 Fall Creators Update and later: Install from the Microsoft Store
This section is for Windows build 16215 or later. Follow these steps to check your build.
- Open the Microsoft Store and choose your favorite Linux distribution.The following links will open the Windows store page for each distribution: Ubuntu
OpenSUSE
SLES
Kali Linux
Debian GNU/Linux
- From the distro’s page, select “Get”
###Complete initialization of your distro Now that your Linux distro is installed, you must initialize your new distro instance once, before it can be used.
Praktikum Pertemuan 2 P
Obkjek di R
Dasar-Dasar R
•Assignment
A<-5 sama dengan A=5 sama dengan 5 -> A
•Case-sensitive
A<-5 berbeda dengan a<-5
•Penamaan objek
-Diawalihuruf(A-Z ataua-z) atautitik(.)
-Tidakmenggunakanspasidankarakterspesial(!,@,#, dst)
-Tidakmenggunakanataumenghindarikata yang sudahdigunakanolehR (NULL, TRUE, FALSE, q, c, t, sin, cos, dll)
•Packages
Moduldi R
•RStudio
Integrated Development Environment (IDE) untuk R
Objek di R
•Vector c(…), seq(…), rep(…), paste(…) •Matrix matrix(…,m,n), dim(vector)<-c(m,n),rbind(…),cbind(…) •Array array(…,dim=c(…)) •Factor factor(…), ordered(…,levels=c(…)) •List list(…) •Data Frame data.frame(…)
Vector
a1<-c(2,4,7,3)
assign("a2",c("2","4","7","3"))
a1## [1] 2 4 7 3
a2## [1] "2" "4" "7" "3"
#baris bilangan
a3<-seq(1,10,by=0.5)
a3## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
## [16] 8.5 9.0 9.5 10.0
a4<-seq(1,10,length.out=12)
a4## [1] 1.000000 1.818182 2.636364 3.454545 4.272727 5.090909 5.909091
## [8] 6.727273 7.545455 8.363636 9.181818 10.000000
#bilangan berulang
a5 <-rep(1,3)
a5## [1] 1 1 1
a6 <-rep(1:3,3)
a6## [1] 1 2 3 1 2 3 1 2 3
a7 <-rep(1:3,1:3)
a7## [1] 1 2 2 3 3 3
a8 <-rep(1:3,rep(2,3))
a8## [1] 1 1 2 2 3 3
a9 <-rep(1:3,each=2)
a9## [1] 1 1 2 2 3 3
#karakterberpola
a10<-paste("A",1:10,sep="")
a10## [1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10"
a11 <-paste0("A",1:10)
a11## [1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10"
a12<-paste("A",1:10,sep="-")
a12## [1] "A-1" "A-2" "A-3" "A-4" "A-5" "A-6" "A-7" "A-8" "A-9" "A-10"
a13<-paste0("A",a8)
a13## [1] "A1" "A1" "A2" "A2" "A3" "A3"
#aksesvector
a2[3]## [1] "7"
a3[10:15]## [1] 5.5 6.0 6.5 7.0 7.5 8.0
a10[c(4,7,9)]## [1] "A4" "A7" "A9"
a13[-c(1:2)]## [1] "A2" "A2" "A3" "A3"
length(a4)## [1] 12
Latihan 1
#tentuukan output
c("la","ye")[rep(c(1,2,2,1),times=4)]## [1] "la" "ye" "ye" "la" "la" "ye" "ye" "la" "la" "ye" "ye" "la" "la" "ye" "ye"
## [16] "la"
c("la","ye")[rep(rep(1:2,each=3),2)]## [1] "la" "la" "la" "ye" "ye" "ye" "la" "la" "la" "ye" "ye" "ye"
Latihan 2
•Buatlah syntaxagar dihasilkan outputvektor sebagai berikut
X1 Y2 X3 Y4 X5 Y6 X7 Y8 X9 Y10
1 4 7 10 13 16 19 22 25 28
lat2a<-(paste0(rep(c("X","Y"),5),1:10))
lat2a## [1] "X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"
lat2b<-seq(1,28,by=3)
lat2b## [1] 1 4 7 10 13 16 19 22 25 28
Matriks
a14 <-1:12
b1<-matrix(a14,3,4)
b1## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
b2 <-matrix(a14,3,4,byrow=TRUE)
b2## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
b3<-matrix(1:10,4,4)## Warning in matrix(1:10, 4, 4): data length [10] is not a sub-multiple or
## multiple of the number of rows [4]
b3## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 3
## [2,] 2 6 10 4
## [3,] 3 7 1 5
## [4,] 4 8 2 6
b4<-matrix(1:10,4,5)
b4## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 3 7
## [2,] 2 6 10 4 8
## [3,] 3 7 1 5 9
## [4,] 4 8 2 6 10
b5 <-a14
dim(b5)<-c(6,2) #merubahobjek vectorke matrixb6 <-matrix(1:4,2)
b7 <-matrix(6:9,2)
b8 <-rbind(b6,b7) #gabungbaris
b9 <-cbind(b7,b6) #gabungkolom
dim(b8)## [1] 4 2
dim(b9)## [1] 2 4
dim(a14)## NULL
length(b3)## [1] 16
#aksesmatrix
b2## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
b2[2,3]## [1] 7
b2[2,2:4]## [1] 6 7 8
b2[1:2,]## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
b2[c(1,3),-2]## [,1] [,2] [,3]
## [1,] 1 3 4
## [2,] 9 11 12
b2[10]## [1] 4
Array
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
c2[,,,2] #buku ke 2 dari c2## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
c2[,,1,3] #lembarke 1 buku ke 3 dari c2## [1] 9 10
Faktor
a15 <-c("A","B","AB","O")
d1 <-factor(a15) #skala pengukuran nominal
d2 <-factor(a15,levels=c("O","A","B","AB"))
levels(d2)## [1] "O" "A" "B" "AB"
a16 <-c("SD","SMP","SMA")
d3 <-ordered(a16) #skala pengukuran ordinal
d4 <-ordered(a16, levels=a16)
d5 <-factor(a16, levels=a16, ordered=TRUE)
levels(d4)## [1] "SD" "SMP" "SMA"
#aksesfactor
d1[2]## [1] B
## Levels: A AB B O
d4[2:3]## [1] SMP SMA
## Levels: SD < SMP < SMA
LIst
a1; b2; c1; d2## [1] 2 4 7 3
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
## , , 1
##
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## , , 2
##
## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
##
## , , 3
##
## [,1] [,2]
## [1,] 9 11
## [2,] 10 12
## [1] A B AB O
## Levels: O A B AB
e1 <-list(a1,b2,c1,d2)
e2 <-list(vect=a1,mat=b2,array=c1,fac=d2)
#akseslist
e1[[1]]## [1] 2 4 7 3
e2$fac## [1] A B AB O
## Levels: O A B AB
e2[2]## $mat
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
e1[c(2,4)]## [[1]]
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
##
## [[2]]
## [1] A B AB O
## Levels: O A B AB
dim(e2)## NULL
length(e2)## [1] 4
names(e2)## [1] "vect" "mat" "array" "fac"
Data Frame
a17 <-11:15
d5 <-factor(LETTERS[6:10])
f1 <-data.frame(d5,a17)
#aksesdata frame
f1[1,2]## [1] 11
f1[3,]## d5 a17
## 3 H 13
f1$d5## [1] F G H I J
## Levels: F G H I J
f1[,"a17"]## [1] 11 12 13 14 15
colnames(f1)## [1] "d5" "a17"
str(f1)## 'data.frame': 5 obs. of 2 variables:
## $ d5 : Factor w/ 5 levels "F","G","H","I",..: 1 2 3 4 5
## $ a17: int 11 12 13 14 15
summary(f1)## d5 a17
## F:1 Min. :11
## G:1 1st Qu.:12
## H:1 Median :13
## I:1 Mean :13
## J:1 3rd Qu.:14
## Max. :15
Pengolahan Objek dan struktur kendali
Pengolahan Objek
x1 <-c(2,6,9,5)
x1## [1] 2 6 9 5
x2 <-1:4
x2## [1] 1 2 3 4
x3 <-x1 + 1:2
x2## [1] 1 2 3 4
x4 <-x1 + 1:4
x4## [1] 3 8 12 9
x5 <-x1*x2
x5## [1] 2 12 27 20
x6 <-x1 %*% x1 #setarax'x
x6## [,1]
## [1,] 146
x7 <-x1 %o% x1 #setaraxx'
x7## [,1] [,2] [,3] [,4]
## [1,] 4 12 18 10
## [2,] 12 36 54 30
## [3,] 18 54 81 45
## [4,] 10 30 45 25
#Operasidasarvektorkarakter:
# nchar(…),paste(…),substr(…),substring(…)
y1 <-c("InstitutPertanianBogor")
n1 <-nchar(y1)
n1## [1] 22
y2 <-c("Adam","Pramesti","Fathi","Ririn")
n2 <-nchar(y2)
n2## [1] 4 8 5 5
y3 <-substr(y1,15,18) #”nian”
y3## [1] "ianB"
y4 <-substring(y1,15) #”nianBogor”
y4## [1] "ianBogor"
y5 <-substring(y1,4,8) #”titut”
y5## [1] "titut"
Struktur Kendali
Eksekusibersyarat
•if (kondisi) ekspresielse ekspresi
•ifelse(kondisi,ekspresibenar,ekspresisalah)
•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)
for (i in 1:5) print(i^2)## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
i<-1
while (i<=5) {
print(i^2)
i=i+1
}## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
y=runif(20)
for (i in y) {
if(i<0.5){
print(100*i)
} else print(i/100)
}## [1] 0.005389451
## [1] 0.007634479
## [1] 16.19016
## [1] 0.00792545
## [1] 25.63307
## [1] 0.007185275
## [1] 1.682251
## [1] 47.26836
## [1] 0.006863742
## [1] 0.005084901
## [1] 9.741865
## [1] 1.951317
## [1] 33.45788
## [1] 0.007761382
## [1] 29.17358
## [1] 22.4096
## [1] 0.007546317
## [1] 1.540109
## [1] 0.007453097
## [1] 0.009411481
z=0
while(z<=10) {
y=runif(20)
z=sum(y)
print(z)
}## [1] 9.752191
## [1] 9.478571
## [1] 10.72605
acak <-sample(1:5,1)
switch(EXPR=acak, "1" = "a", "2" = "z",
"3" = "m", "4" = "h", "5" = "t")## [1] "a"
Z6 <-matrix(1:25,5,5)
apply(Z6,1,sum)## [1] 55 60 65 70 75
apply(Z6,2,sd)## [1] 1.581139 1.581139 1.581139 1.581139 1.581139
Latihan
a<-0
for(i in 1:5) { b<-a+i
print(b)
a<-b
}## [1] 1
## [1] 3
## [1] 6
## [1] 10
## [1] 15
i<-1
z<-1
while(z<15)
{y<-z+i
z<-y
print(z)
i<-i+1
}## [1] 2
## [1] 4
## [1] 7
## [1] 11
## [1] 16
i<-1
m<-2
repeat
{m<-m+i
print(m)
i<-i+1
if(m>15)
break
}## [1] 3
## [1] 5
## [1] 8
## [1] 12
## [1] 17
Praktikum Pertemuan 3
Manajemen Data Frame(Data Wrangling/Munging):
• Membuat peubah baru dalam data frame
• Subsetting data
• Sorting data
• Recoding data
• Merging data
• Reshaping data
Data
Seorang peneliti merancang sebuah perancangan percobaan RAKL dengan 4 perlakuan dan 3 kelompok (anggaplah respon percobaan berupa baris bilangan), kemudian disimpan dalam objek data1.
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)
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
## 7 P3 1 13
## 8 P3 2 15
## 9 P3 3 17
## 10 P4 1 19
## 11 P4 2 21
## 12 P4 3 23
Membuat Peubah Baru dalam Data Frame
•Dilakukan seperti membuat vektor (dengan indeks atau operasi seleksi)
namadataframe$namavariabelbaru<-ekspresi
namadataframe[,nomorkolom]<-ekspresi
Latihan 1
Pada data1, buatlah peubah’baru1’yang berisi nilai dari 12 sampai 1 secara berurutan
data1$baru1<-12:1
data1## Perl Kel Resp baru1
## 1 P1 1 1 12
## 2 P1 2 3 11
## 3 P1 3 5 10
## 4 P2 1 7 9
## 5 P2 2 9 8
## 6 P2 3 11 7
## 7 P3 1 13 6
## 8 P3 2 15 5
## 9 P3 3 17 4
## 10 P4 1 19 3
## 11 P4 2 21 2
## 12 P4 3 23 1
Subsetting Data
•Dilakukan untuk akses sebagian data
•Membuat ide logicuntuk diterapkan dalam vektor logic yang diinginkan
•Fungsi yang digunakan : ==, !=, >, >=, <, <=, %in%, duplicated(…),which(…),is.na(…), is.null(…), is.numeric(…), dll…
Latihan 2
Dari data1 tersebut ambillah yang termasuk kelompok 1
indeks1 <-data1$Kel == 1
data2 <-data1[indeks1,]
data2## Perl Kel Resp baru1
## 1 P1 1 1 12
## 4 P2 1 7 9
## 7 P3 1 13 6
## 10 P4 1 19 3
Latihan 3
Dari data1tersebut ambillah yang termasuk kelompok 1 atau perlakuan P2
indeks2 <-data1$Kel == 1 | data1$Perl == "P2"
data3 <-data1[indeks2,]
data3## Perl Kel Resp baru1
## 1 P1 1 1 12
## 4 P2 1 7 9
## 5 P2 2 9 8
## 6 P2 3 11 7
## 7 P3 1 13 6
## 10 P4 1 19 3
Latihan 4
Dari data1tersebut ambillah amatan yang responnya berupa bilangan prima
indeks3 <-data1$Resp %in% c(2,3,5,7,11,13,17,19,23)
data4 <-data1[indeks3,]
data4## Perl Kel Resp baru1
## 2 P1 2 3 11
## 3 P1 3 5 10
## 4 P2 1 7 9
## 6 P2 3 11 7
## 7 P3 1 13 6
## 9 P3 3 17 4
## 10 P4 1 19 3
## 12 P4 3 23 1
Sorting Data
•Dilakukan untuk mengurutkan data berdasarkan beberapa peubahtertentu
•Dilakukan dengan membuat vektor logika untuk melakukan pengurutan data
•Fungsi yang sering digunakan order(…), sort(…), rev(…), unique(…)
Latihan 5
Urutkan data1 tersebut berdasarkan kelompok secara ascending
indeks4 <-order(data1$Kel)
data5 <-data1[indeks4,]
data5## Perl Kel Resp baru1
## 1 P1 1 1 12
## 4 P2 1 7 9
## 7 P3 1 13 6
## 10 P4 1 19 3
## 2 P1 2 3 11
## 5 P2 2 9 8
## 8 P3 2 15 5
## 11 P4 2 21 2
## 3 P1 3 5 10
## 6 P2 3 11 7
## 9 P3 3 17 4
## 12 P4 3 23 1
Latihan 6
Urutkan data1tersebut berdasarkan kelompok dan respon secara descending
indeks5 <-order(data1$Kel, data1$Resp, decreasing=TRUE)
data6 <-data1[indeks5,]
data6## Perl Kel Resp baru1
## 12 P4 3 23 1
## 9 P3 3 17 4
## 6 P2 3 11 7
## 3 P1 3 5 10
## 11 P4 2 21 2
## 8 P3 2 15 5
## 5 P2 2 9 8
## 2 P1 2 3 11
## 10 P4 1 19 3
## 7 P3 1 13 6
## 4 P2 1 7 9
## 1 P1 1 1 12
Latihan 7
Urutkan data1tersebut berdasarkan kelompok secara ascending dan respon secara descending
indeks6 <-order(data1$Resp, decreasing=TRUE)
data7 <-data1[indeks6,]
indeks7 <-order(data7$Kel)
data8 <-data7[indeks7,]
data8## Perl Kel Resp baru1
## 10 P4 1 19 3
## 7 P3 1 13 6
## 4 P2 1 7 9
## 1 P1 1 1 12
## 11 P4 2 21 2
## 8 P3 2 15 5
## 5 P2 2 9 8
## 2 P1 2 3 11
## 12 P4 3 23 1
## 9 P3 3 17 4
## 6 P2 3 11 7
## 3 P1 3 5 10
Sorting Data
data8$Resp ## [1] 19 13 7 1 21 15 9 3 23 17 11 5
sort(data8$Resp) ## [1] 1 3 5 7 9 11 13 15 17 19 21 23
rev(data8$Resp)## [1] 5 11 17 23 3 9 15 21 1 7 13 19
order(data8$Resp)## [1] 4 8 12 3 7 11 2 6 10 1 5 9
rank(data8$Resp)## [1] 10 7 4 1 11 8 5 2 12 9 6 3
data8$Resp>10## [1] TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE
which(data8$Resp>10)## [1] 1 2 5 6 9 10 11
data8$Resp[data8$Resp>10]## [1] 19 13 21 15 23 17 11
data8$Resp[which(data8$Resp>10)]## [1] 19 13 21 15 23 17 11
Recording Data
•Digunakan untuk membuat nilai baru dari nilai peubahyang sudah ada
•Dapatdilakukansecara logical, fungsi ifelse(…), dan fungsi recode(…)
Latihan 8
•Lakukanlah recoding pada data8 untuk variabel respon dengan kondisi jika respon < 15 maka Code = 1, selainnya Code = 0
#dengan logical
data8$Code1 <-0*(data8$Resp>=15) + 1*(data8$Resp<15)
data8$Code1## [1] 0 1 1 1 0 0 1 1 0 0 1 1
#denganfungsiifelse
data8$Code2 <-ifelse(data8$Resp<15,1,0)
data8$Code2## [1] 0 1 1 1 0 0 1 1 0 0 1 1
#dengan fungsi recode
library(car)## Loading required package: carData
data8$Code3 <-recode(data8$Resp,'1:14=1; else=0')
data8$Code3## [1] 0 1 1 1 0 0 1 1 0 0 1 1
Merging Data
•Bisa dilakukan dengan rbind(…)atau cbind(…)
•Lebih mudah dengan fungsi merge(…)
Latihan 9
Gabungkanlah data1 dengan tabel1 berdasarkan peubah pertamanya
tabel1 <- data.frame(Tr=c("P4","P2","P5"), k1=c(50,100,200))
tabel1## Tr k1
## 1 P4 50
## 2 P2 100
## 3 P5 200
data9<-merge(data1, tabel1, by.x=1, by.y=1, all=FALSE)
data10<-merge(data1, tabel1, by.x="Perl",by.y="Tr",all=TRUE)
data10## Perl Kel Resp baru1 k1
## 1 P1 1 1 12 NA
## 2 P1 2 3 11 NA
## 3 P1 3 5 10 NA
## 4 P2 3 11 7 100
## 5 P2 1 7 9 100
## 6 P2 2 9 8 100
## 7 P3 2 15 5 NA
## 8 P3 3 17 4 NA
## 9 P3 1 13 6 NA
## 10 P4 1 19 3 50
## 11 P4 2 21 2 50
## 12 P4 3 23 1 50
## 13 P5 <NA> NA NA 200
Reshaping Data
•Membentuk data baru dengan cara :
•Long towideformat
•Widetolongformat
•Menggunakan fungsi reshape(…)
Latihan 10
Ubahlah data1 menjadi data dengan setiap barisnya merupakan masing-masing perlakuan
data1## Perl Kel Resp baru1
## 1 P1 1 1 12
## 2 P1 2 3 11
## 3 P1 3 5 10
## 4 P2 1 7 9
## 5 P2 2 9 8
## 6 P2 3 11 7
## 7 P3 1 13 6
## 8 P3 2 15 5
## 9 P3 3 17 4
## 10 P4 1 19 3
## 11 P4 2 21 2
## 12 P4 3 23 1
#long to wide
data11 <-reshape(data1[,-4], idvar="Perl", timevar="Kel", direction="wide")
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
#wide to long
data12 <-reshape(data11, idvar="Perl", timevar="Kel", direction="long")
data12## 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
## P3.2 P3 2 15
## P4.2 P4 2 21
## P1.3 P1 3 5
## P2.3 P2 3 11
## P3.3 P3 3 17
## P4.3 P4 3 23
Demikian, Terima Kasih
Satria June Adwendi, IPB University, sjadwendi@apps.ipb.ac.id↩︎