This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the plot.
=== Belajar Array dengan Bahasa R ===
Jadi, apa itu array ?
Array adalah kumpulan variabel yang mempunyai tipe data yang sama, seperti angka dengan angka maupun huruf dengan huruf.lewat array kita bisa menyimpan sekumpulan data,dan array dibagi menjadi dua yakni array 1 dimensi dan array multidimensi
A.Syntax untuk keluarkan Input array
Housing = read.csv("http://www.mosaic-web.org/go/datasets/Income-Housing.csv")
data1 <- c(0,7,3)
data2 <- c(12,4.5,99,3.0,1,1)
result <- array(c(data1,data2),dim = c(3,3,2))
print(result)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0 12.0 3
## [2,] 7 4.5 1
## [3,] 3 99.0 1
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0 12.0 3
## [2,] 7 4.5 1
## [3,] 3 99.0 1
B.penamaan kolom dan baris
data1<- c(5,9,3)
data2 <- c(10,11,12,13,14,15)
column.names <- c("Kol1","KOL2","KOL3")
row.names <- c("Bar1","Bar2","Bar3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(data1,data2),dim = c(3,3,2),dimnames = list(row.names,column.names,
matrix.names))
print(result)
## , , Matrix1
##
## Kol1 KOL2 KOL3
## Bar1 5 10 13
## Bar2 9 11 14
## Bar3 3 12 15
##
## , , Matrix2
##
## Kol1 KOL2 KOL3
## Bar1 5 10 13
## Bar2 9 11 14
## Bar3 3 12 15
C.cara mengakses elemen Array
data1 <- c(1,2,3)
data2 <- c(10,11,12,13,14,15)
column.names <- c("KOL1","KOL2","KOL3")
row.names <- c("BAR1","BAR2","BAR3")
matrix.names <- c("Matrix1","Matrix2")
result <- array(c(data1,data2),dim = c(3,3,2),dimnames = list(row.names,
column.names, matrix.names))
print(result[3,,2])
## KOL1 KOL2 KOL3
## 3 12 15
print(result[1,3,1])
## [1] 13
print(result[,,2])
## KOL1 KOL2 KOL3
## BAR1 1 10 13
## BAR2 2 11 14
## BAR3 3 12 15
D.Memanipulasi Elemen array
data1 <- c(1,9,3)
data2 <- c(10,11,12,13,14,15)
array1 <- array(c(data1,data2),dim = c(3,3,2))
data3 <- c(9,1,0)
data4 <- c(6,0,11,3,14,1,2,6,9)
array2 <- array(c(data3,data4),dim = c(3,3,2))
matrix1 <- array1[,,2]
matrix2 <- array2[,,2]
result <- matrix1+matrix2
print(result)
## [,1] [,2] [,3]
## [1,] 3 19 19
## [2,] 15 12 14
## [3,] 12 12 26
E.Perhitungan dalam Array Fungsi Apply(),digunakan untuk untuk perhitungan array dengan Syntax :
apply(x, margin, fun)
x untuk array margin untuk nama kumpulan data fun untuk fungsi yang akan diterapkan di array
data1 <- c(1,2,3)
data2 <- c(10,11,12,13,14,15)
new.array <- array(c(data1,data2),dim = c(3,3,3))
print(new.array)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 10 13
## [2,] 2 11 14
## [3,] 3 12 15
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 10 13
## [2,] 2 11 14
## [3,] 3 12 15
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 1 10 13
## [2,] 2 11 14
## [3,] 3 12 15
result <- apply(new.array, c(1), sum)
print(result)
## [1] 72 81 90