Laporan Praktikum 01

Algoritma dan Pemrograman dalam Bahasa R

Dasar R dan Operasi Aritmetika

Mendefenisikan sebuah objek dalam bahasa R bisa menggunakan <-atau =.

#Membuat Objek
a <- 4
b <- 7

#OPERASI MATEMATIKA
a+b
## [1] 11
a = 4
b = 7
a+b
## [1] 11
a*b^2
## [1] 196
a/b
## [1] 0.5714286
b%%a
## [1] 3
#Objek menyimpan lebih dari satu nilai
a = c(1,2,4,5)
b = c(6,7,9,2)
a*b
## [1]  6 14 36 10
#C - Fungsi CONCATENATE
d = c(a,b)
d
## [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

x = c(2,3,4,5)
x
## [1] 2 3 4 5
y = c("sd","smp","sma")
y
## [1] "sd"  "smp" "sma"
y[1] # tanda kurung siku untuk memunculkan elemen ke-i
## [1] "sd"
y[c(1:3)] # satu sampai tiga
## [1] "sd"  "smp" "sma"
y[c(1,3)] #elemen ke satu dan tiga
## [1] "sd"  "sma"
y[4]
## [1] NA
#named number
nilai <- c(statistik = 89, fisika = 95, ilmukomunikasi = 100)
nilai["fisika"]
## 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

A = matrix(c(1,2,3,4),nrow=2,ncol=2,byrow=TRUE)
A
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
A[1,1]
## [1] 1
A[,2]
## [1] 2 4

3. FACTOR DAN MENGAKSESNYA

Factor adalah tipe khusus vektor dengan elemen data kategori, vektor yang mengindikasikan taraf(level) dari peubah kategori.

y = c("sd","smp","sma")
y
## [1] "sd"  "smp" "sma"
x = factor(y)
x
## [1] sd  smp sma
## Levels: sd sma smp
x = factor(y, levels = y)
x
## [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
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

5. LIST DAN MENGAKSESNYA

List adalah objek data yang elemen didalamnya boleh memiliki mode atau objek yang berbeda.

X = list(1:12, c(1,2,3), c("a","b"), A=matrix(1:4, nrow=2, ncol=2))
X
## [[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
X[[3]]
## [1] "a" "b"
X$A
##      [,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
#Menampilkan kolom jumlah_mahasiswa
info_mahasiswa$jumlah_mahasiswa
## [1] 260  28 284 465 735
#Menampilkan kolom fakultas
info_mahasiswa$fakultas
## [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.

library(readxl)
## Warning: package 'readxl' was built under R version 4.0.4
auto <- read_excel("D:/auto.xlsx")
str(auto)
## 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
# MENGHAPUS VARIABEL
auto = auto[,c(-10)]
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
## 1 chevrolet chevelle malibu
## 2         buick skylark 320
## 3        plymouth satellite
## 4             amc rebel sst
## 5               ford torino
## 6          ford galaxie 500
# MENYUSUN ULANG URUTAN KOLOM
auto = auto[,c(9,1,2,3,4,5,6,7,8)]
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
## 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
# MEMBUAT VARIABEL LN
lnmpg = log(auto$mpg)
head(lnmpg)
## [1] 2.890372 2.708050 2.890372 2.772589 2.833213 2.708050
auto = cbind(auto,lnmpg)
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
## 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
auto = auto[,c(-9)]
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    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
# MENGHAPUS OBSERVASI DENGAN MISSING VALUE
autonew = na.omit(auto)
head(autonew)
##                        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

#while
i=1
while(i<=5){
  print("Hello")
  i=i+1
}
## [1] "Hello"
## [1] "Hello"
## [1] "Hello"
## [1] "Hello"
## [1] "Hello"
#menuliskan 1-10
#while loop
a=1
while (a<=10) {
  print(a)
  a=a+1
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
#menuliskan bilangan genap 1-10
a=1
while(a<=10) {
  if(a%%2==0) {
    print(a)
  }
  a=a+1
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10

2. For Loop

my_vektor = c(2,4,6,8,10)
for (nilai in my_vektor) {
  print(nilai)
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
#duaiterasi forloop
for (i in 1:10) {
  for(j in 1:i) {
    cat(j)
  }
  cat("\n")
}
## 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"
#menggunakan NEXT
for (i in 1:10) {
  if ( i == 4 ) {
    next
  }
  print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10

3. Repeat

coba <- c('Hello')
x <- 1
repeat {
  print(coba)
  x <- x + 1
  if(x < 5){
    break
  }
}
## [1] "Hello"

Statistika Deskriptif

summary(auto$mpg)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    9.00   17.50   23.00   23.52   29.00   46.60
mean(auto$mpg)
## [1] 23.51587
# BOXPLOT
boxplot(auto$mpg)

boxplot(auto$mpg, main="Boxplot mpg")

# HISTOGRAM
hist(auto$mpg, main="Histogram mpg", col="yellow")

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")

plot(auto$mpg, auto$displacement, xlab="mpg mobil", ylab="displacement", type="l")

# MENGHITUNG KORELASI LINIER (PEARSON) DARI SEMUA VARIABEL NUMERIK
cor(auto[,c(-1)])
##                     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
korelasi = cor(autonew[,c(-1)])
round(korelasi,3)
##                 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
# TABULASI SILANG
mytable <- table(autonew$cylinders,autonew$asal) 
mytable 
##    
##       1   2   3
##   3   0   0   4
##   4  69  61  69
##   5   0   3   0
##   6  73   4   6
##   8 103   0   0
# ANOVA
anova(lm(auto$mpg ~ factor(auto$asal)))
## 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
# KORELASI PEARSON DAN SPEARMAN DARI 2 VARIABEL
cor(auto[,c(3,10)], method="spearman")
##            cylinders       asal
## cylinders  1.0000000 -0.6071163
## asal      -0.6071163  1.0000000
cor(auto[,c(3,10)], method="pearson")
##            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
#Menambah perubah baru pada data1
data1$baru1 <- 1:12
head(data1)
##   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…

#Subset kelompok 1
indeks1 <-data1$Kel == 1
data2 <-data1[indeks1,]
head(data2)
##    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
indeks7 <-order(data7$Kel)
data8 <-data7[indeks7,]
head(data8)
##    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
#Lainnya
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

Recoding Data

•Digunakan untuk membuat nilai baru dari nilai peubah yang sudah ada

•Dapat dilakukan secara logical, fungsi ifelse(), dan fungsi recode()

#denganlogical
data8$Code1 <-0*(data8$Resp>=15) + 1*(data8$Resp<15)
head(data8)
##    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
#denganfungsiifelse
data8$Code2 <-ifelse(data8$Resp<15,1,0)
head(data8)
##    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
#denganfungsirecode
library(car)
## Loading required package: carData
data8$Code3 <-recode(data8$Resp,'1:14=1; else=0')
head(data8)
##    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
data10 <- merge(data1, tabel1, by.x="Perl",by.y="Tr",all=TRUE)
head(data10)
##   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
#wide to long
data12 <-reshape(data11, idvar="Perl", timevar="Kel", direction="long")
head(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

Terimakasih