Anda di halaman 1dari 4

PERTEMUAN II

(Data Frame dan Matrix)


1. Data Frame
2.1 Operators
> a = 1:5
> b = 10:5
> a = 1:5
> b = 10:6
> level = c("A", "B", "C", "D", "E")
> rian = data.frame(posisi=a, waktu=b, nilai=level)
> rian
posisi waktu nilai
1 1 10 A
2 2 9 B
3 3 8 C
4 4 7 D
5 5 6 E
> rian$posisi
[1] 1 2 3 4 5
> rian$waktu
[1] 10 9 8 7 6
> rian$nilai
[1] A B C D E
Levels: A B C D E
2. Matrices
> a = matrix(c(1,2,3,4), nrow=2)
> a
[,1] [,2]
[1,] 1 3
[2,] 2 4
> a = matrix(1:3, nrow=3, ncol=3)
> a
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
> a = matrix(1:3, nrow=3, ncol=3, byrow=T)
> a
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3
> t(matrix(1:3, nrow=3, ncol=3))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3

> b = matrix(c(1,2,3,4), nrow=2)


> c = matrix(c(3,4), nrow=2)
> b%*%c
[,1]
[1,] 15
[2,] 22
> det(a)
[1] 0
> det(b)
[1] -2
> diag(c(1,2))
[,1] [,2]
[1,] 1 0
[2,] 0 2
> diag(1:4)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 2 0 0
[3,] 0 0 3 0
[4,] 0 0 0 4
> diag(rep(1,2))
[,1] [,2]
[1,] 1 0
[2,] 0 1
> diag(2)
[,1] [,2]
[1,] 1 0
[2,] 0 1
> diag(5)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
Untuk mendapatkan informasi terkait dengan framework dan beberapa operator dalam
matrix anda dapat melihatnya di library(help=Matrix)
Fungsi cbind digunakan untuk menggambungkan data berdasarkan kolom dan fungsi
rbind digunakan untuk menggabungkan data berdasarkan baris
> cbind( c(1,2), c(3,4) )
[,1] [,2]
[1,] 1 3
[2,] 2 4
> rbind( c(1,2), c(3,4) )
[,1] [,2]
[1,] 1 2

[2,] 3 4
Menjumlahkan data diagonal matrik
> sum(diag(b))
[1] 5
Menginverskan matrix
> solve(b)
[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5
Games :
> plot(cbind(c(1,4,4,1,1),c(1,1,4,4,1)), type="l")
> plot(cbind(c(1,3,5,1),c(1,4,1,1)), type="l")