Terdapat empat buah mode vektor dalam R, yaitu ‘logical’, ‘numeric’, ‘character’, dan ‘list’. Sederhananya, keempat mode tersebut dapat terjelaskan dari contoh syntax berikut.
> c(4,3,4,5,6,4,3,4,5,6,3) #logical
[1] 4 3 4 5 6 4 3 4 5 6 3
> c(1:11) #numeric
[1] 1 2 3 4 5 6 7 8 9 10 11
> c(F,F,T,T,F,T,F,T,T,F,F) #character
[1] FALSE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE
> c("Joni","Anton","Budi","Ria","Syifa","Dona","Indri","Yani","Purnama","Maulana","Jojon") #list
[1] "Joni" "Anton" "Budi" "Ria" "Syifa" "Dona" "Indri" "Yani" "Purnama"
[10] "Maulana" "Jojon"
Tentunya ketika mode vector tersebut dapat digabungkan dalam sebuah data frame. Dengan catatan jumlah row harus sama, berikut contohnya :
logical<-c(4,3,4,5,6,4,3,4,5,6,3) #logical
> numeric<-1:15 #numeric
> character<-c(F,F,T,T,F,T,F,T,T,F,F)
> list<-c("Joni","Anton","Budi","Ria","Syifa","Dona","Indri","Yani","Purnama","Maulana","Jojon")
> mode.data<-data.frame(Logical=logical,Numeric=numeric,Character=character,List=list)
Error in data.frame(Logical = logical, Numeric = numeric, Character = character, :
arguments imply differing number of rows: 11, 15 #Error karena jumlah row berbeda, saya kembali mengedit numeric arga jumlah row sama.
> numeric<-1:11 #numeric
> mode.data<-data.frame(Logical=logical,Numeric=numeric,Character=character,List=list)
> mode.data
Logical Numeric Character List
1 4 1 FALSE Joni
2 3 2 FALSE Anton
3 4 3 TRUE Budi
4 5 4 TRUE Ria
5 6 5 FALSE Syifa
6 4 6 TRUE Dona
7 3 7 FALSE Indri
8 4 8 TRUE Yani
9 5 9 TRUE Purnama
10 6 10 FALSE Maulana
11 3 11 FALSE Jojon
Selain itu Mode tersebut dapat digabungkan dalam satu vector, berikut contohnya :
vector.gabung<-c(logical,numeric,character,list)
> vector.gabung
[1] "4" "3" "4" "5" "6" "4" "3" "4" "5"
[10] "6" "3" "1" "2" "3" "4" "5" "6" "7"
[19] "8" "9" "10" "11" "FALSE" "FALSE" "TRUE" "TRUE" "FALSE"
[28] "TRUE" "FALSE" "TRUE" "TRUE" "FALSE" "FALSE" "Joni" "Anton" "Budi"
[37] "Ria" "Syifa" "Dona" "Indri" "Yani" "Purnama" "Maulana" "Jojon"
Fungsi lain yang dirasa perlu adalah mengambil sebagian informasi dari vector, berikut adalah contohnya:
vector.gabung[c(3,28)] #Mengambil posisi ke-3 dan 20
[1] "4" "TRUE"
vector.gabung[-c(3:20)] #Mengambil posisi ke-3 dan 20 #menghapus posisi ke 3 sampai 20
[1] "4" "3" "10" "11" "FALSE" "FALSE" "TRUE" "TRUE" "FALSE"
[10] "TRUE" "FALSE" "TRUE" "TRUE" "FALSE" "FALSE" "Joni" "Anton" "Budi"
[19] "Ria" "Syifa" "Dona" "Indri" "Yani" "Purnama" "Maulana" "Jojon"
Selain itu vector bisa difilter menggunakan logical value, berikut adalah contohnya :
vector.gabung>5
[1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
[16] FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
________________________________
Referensi : Data Analysis and Graphics Using R – an Example-based Approach (John Maindonald & John Braun)
Leave a Reply