x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))
c(10.4, 5.6, 3.1, 6.4, 21.7)-> x
1/x
## [1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295
y <- c(x, 0, x)
x
## [1] 10.4 5.6 3.1 6.4 21.7
y
## [1] 10.4 5.6 3.1 6.4 21.7 0.0 10.4 5.6 3.1 6.4 21.7
v <- 2*x+y+1
## Warning in 2 * x + y: longer object length is not a multiple of shorter object
## length
sum((x-mean(x))^2)/(length(x)-1)
## [1] 53.853
sqrt(-17)
## Warning in sqrt(-17): NaNs produced
## [1] NaN
sqrt(-17+0i)
## [1] 0+4.123106i
seq(-5, 5, by=.2)-> s3
s4 <- seq(length=51, from=-5, by=.2)
s5 <- rep(x, times=5)
s6 <- rep(x, each=5)
s4
## [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2
## [16] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8
## [31] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8
## [46] 4.0 4.2 4.4 4.6 4.8 5.0
s5
## [1] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7
## [16] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7
s6
## [1] 10.4 10.4 10.4 10.4 10.4 5.6 5.6 5.6 5.6 5.6 3.1 3.1 3.1 3.1 3.1
## [16] 6.4 6.4 6.4 6.4 6.4 21.7 21.7 21.7 21.7 21.7
temp <- x > 13
z <- c(1:3,NA); ind <- is.na(z)
0/0
## [1] NaN
Inf- Inf
## [1] NaN
labs <- paste(c("X","Y"), 1:10, sep="")
c("X1", "Y2", "X3", "Y4", "X5", "Y6", "X7", "Y8", "X9", "Y10")
## [1] "X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"
y <- x[!is.na(x)]
(x+1)[(!is.na(x)) & x>0]-> z
x[1:10]
## [1] 10.4 5.6 3.1 6.4 21.7 NA NA NA NA NA
c("x","y")[rep(c(1,2,2,1), times=4)]
## [1] "x" "y" "y" "x" "x" "y" "y" "x" "x" "y" "y" "x" "x" "y" "y" "x"
y <- x[-(1:5)]
fruit <- c(5, 10, 1, 20)
names(fruit) <- c("orange", "banana", "apple", "peach")
lunch <- fruit[c("apple","orange")]
x[is.na(x)] <- 0
y[y < 0] <--y[y < 0]
y <- abs(y)
Matrices, factors, lists, data frames, functions
z <- 0:9
digits <- as.character(z)
d <- as.integer(digits)
e <- numeric()
e[3] <- 17
e
## [1] NA NA 17
alpha <- 1:10 # Variabel pendukung: alpha panjang 10
alpha <- alpha[2 * 1:5]
alpha
## [1] 2 4 6 8 10
length(alpha) <- 3
alpha
## [1] 2 4 6
# Menyiapkan variabel z berukuran 100 elemen
z <- 1:100
# Memberikan atribut 'dim' agar z diperlakukan sebagai matriks 10x10
attr(z, "dim") <- c(10, 10)
attributes(z)
## $dim
## [1] 10 10
z
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 11 21 31 41 51 61 71 81 91
## [2,] 2 12 22 32 42 52 62 72 82 92
## [3,] 3 13 23 33 43 53 63 73 83 93
## [4,] 4 14 24 34 44 54 64 74 84 94
## [5,] 5 15 25 35 45 55 65 75 85 95
## [6,] 6 16 26 36 46 56 66 76 86 96
## [7,] 7 17 27 37 47 57 67 77 87 97
## [8,] 8 18 28 38 48 58 68 78 88 98
## [9,] 9 19 29 39 49 59 69 79 89 99
## [10,] 10 20 30 40 50 60 70 80 90 100
mtcars
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
unclass(mtcars)
## $mpg
## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
##
## $cyl
## [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
##
## $disp
## [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8
## [13] 275.8 275.8 472.0 460.0 440.0 78.7 75.7 71.1 120.1 318.0 304.0 350.0
## [25] 400.0 79.0 120.3 95.1 351.0 145.0 301.0 121.0
##
## $hp
## [1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230 66 52
## [20] 65 97 150 150 245 175 66 91 113 264 175 335 109
##
## $drat
## [1] 3.90 3.90 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 3.92 3.07 3.07 3.07 2.93
## [16] 3.00 3.23 4.08 4.93 4.22 3.70 2.76 3.15 3.73 3.08 4.08 4.43 3.77 4.22 3.62
## [31] 3.54 4.11
##
## $wt
## [1] 2.620 2.875 2.320 3.215 3.440 3.460 3.570 3.190 3.150 3.440 3.440 4.070
## [13] 3.730 3.780 5.250 5.424 5.345 2.200 1.615 1.835 2.465 3.520 3.435 3.840
## [25] 3.845 1.935 2.140 1.513 3.170 2.770 3.570 2.780
##
## $qsec
## [1] 16.46 17.02 18.61 19.44 17.02 20.22 15.84 20.00 22.90 18.30 18.90 17.40
## [13] 17.60 18.00 17.98 17.82 17.42 19.47 18.52 19.90 20.01 16.87 17.30 15.41
## [25] 17.05 18.90 16.70 16.90 14.50 15.50 14.60 18.60
##
## $vs
## [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1
##
## $am
## [1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1
##
## $gear
## [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
##
## $carb
## [1] 4 4 1 1 2 1 4 2 2 4 4 3 3 3 4 4 4 1 2 1 1 2 2 4 2 1 2 2 4 6 8 2
##
## attr(,"row.names")
## [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
## [4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
## [7] "Duster 360" "Merc 240D" "Merc 230"
## [10] "Merc 280" "Merc 280C" "Merc 450SE"
## [13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
## [16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
## [19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
## [22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
## [25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
## [28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
## [31] "Maserati Bora" "Volvo 142E"
state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa", "qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas", "sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa", "sa", "act", "nsw", "vic", "vic", "act")
statef <- factor(state)
statef
## [1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa tas sa nt wa
## [20] vic qld nsw nsw wa sa act nsw vic vic act
## Levels: act nsw nt qld sa tas vic wa
levels(statef)
## [1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa"
incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56, 61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46, 59, 46, 58, 43)
incmeans <- tapply(incomes, statef, mean)
stdError <- function(x) sqrt(var(x)/length(x))
incster <- tapply(incomes, statef, stdError)
incster
## act nsw nt qld sa tas vic wa
## 1.500000 4.310195 4.500000 4.106093 2.738613 0.500000 5.244044 2.657536
level faktor disesuaikan sama urutan alfabet, kalo mau berurutan pakai fungsi ordered()
z <- 1:1500
dim(z) <- c(3,5,100)
# Membuat isi elemen bernilai 1 sampai 24
data_vec <- 1:24
# Membuat array dengan dimensi 3 x 4 x 2
a <- array(data_vec, dim = c(3, 4, 2))
# Menampilkan struktur array
a
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 13 16 19 22
## [2,] 14 17 20 23
## [3,] 15 18 21 24
c(a[2,1,1], a[2,2,1], a[2,3,1], a[2,4,1], a[2,1,2], a[2,2,2], a[2,3,2], a[2,4,2])
## [1] 2 5 8 11 14 17 20 23
# 1. Buat array x berukuran 4x5 dengan nilai 1 sampai 20
x <- array(1:20, dim = c(4, 5))
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
# 2. Buat matriks indeks i berukuran 3x2 untuk memilih koordinat [1,3], [2,2], dan [3,1]
i <- array(c(1:3, 3:1), dim = c(3, 2))
i
## [,1] [,2]
## [1,] 1 3
## [2,] 2 2
## [3,] 3 1
# 3. Ekstrak elemen-elemen dari x menggunakan matriks indeks i
x[i]
## [1] 9 6 3
# 4. Ganti nilai elemen-elemen tersebut menjadi 0
x[i] <- 0
# 5. Tampilkan matriks x yang sudah diperbarui
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 0 13 17
## [2,] 2 0 10 14 18
## [3,] 0 7 11 15 19
## [4,] 4 8 12 16 20
# Asumsi: n=6 plot, b=2 blok, v=3 varietas
n <- 6
blocks <- factor(c(1, 1, 1, 2, 2, 2))
varieties <- factor(c(1, 2, 3, 1, 2, 3))
b <- length(levels(blocks))
v <- length(levels(varieties))
#1. Inisialisasi matriks nol untuk Block (Xb) dan Variety (Xv)
Xb <- matrix(0, n, b)
Xv <- matrix(0, n, v)
#2. Buat matriks indeks koordinat
ib <- cbind(1:n, blocks)
iv <- cbind(1:n, varieties)
#3. Isi nilai 1 pada koordinat indeks yang ditentukan
Xb[ib] <- 1
Xv[iv] <- 1
#4. Gabungkan Xb dan Xv menjadi Design Matrix (X)
X <- cbind(Xb, Xv)
#5. Buat Incidence Matrix (N) menggunakan perkalian silang (crossprod)
N <- crossprod(Xb, Xv)
#6. alternatif yang lebih sederhana membuat Incidence Matrix (N)
N_simpler <- table(blocks, varieties)
# Variabel h berukuran 24 elemen
h <- 1:24
# Membuat array Z berukuran 3 x 4 x 2
Z <- array(h, dim = c(3, 4, 2))
# Cara alternatif mengatur dimensi
Z <- h; dim(Z) <- c(3, 4, 2)
# Mengisi seluruh array dengan angka 0
Z <- array(0, c(3, 4, 2))
# Operasi aritmatika pada array (variabel A, B, C dibuatkan berukuran sama)
A <- array(1:24, dim = c(3, 4, 2))
B <- array(25:48, dim = c(3, 4, 2))
C <- array(49:72, dim = c(3, 4, 2))
D <- 2 * A * B + C + 1
# Variabel a dan b untuk outer product
a <- 1:3
b <- 1:4
# Outer product menggunakan operator %o% dan fungsi outer()
ab <- a %o% b
ab <- outer(a, b, "*")
# Evaluasi fungsi f(x, y) = cos(y) / (1 + x^2) pada grid x dan y
x <- seq(-2, 2, length.out = 5) # Variabel pendukung x
y <- seq(-2, 2, length.out = 5) # Variabel pendukung y
f <- function(x, y) cos(y)/(1 + x^2)
z <- outer(x, y, f)
# Contoh: Determinan matriks 2x2 dari digit tunggal
d <- outer(0:9, 0:9)
fr <- table(outer(d, d, "-"))
plot(fr, xlab = "Determinant", ylab = "Frequency")
# Transpose tergeneralisasi (aperm)
A <- matrix(1:6, nrow = 2, ncol = 3) # Variabel matriks A
B <- aperm(A, c(2, 1))
# Sama hasilnya dengan: B <- t(A)
# Menyiapkan matriks A dan B kuadrat berukuran sama (2x2)
A <- matrix(c(4, 3, 3, 2), nrow = 2)
B <- matrix(c(1, 2, 3, 4), nrow = 2)
# Transpose, nrow, ncol
t(A)
## [,1] [,2]
## [1,] 4 3
## [2,] 3 2
nrow(A)
## [1] 2
ncol(A)
## [1] 2
# Perkalian elemen-demi-elemen vs Perkalian Matriks
A * B
## [,1] [,2]
## [1,] 4 9
## [2,] 6 8
A %*% B
## [,1] [,2]
## [1,] 10 24
## [2,] 7 17
# Bentuk kuadratik (Quadratic form): x^T %*% A %*% x
x <- c(1, 2)
x %*% A %*% x
## [,1]
## [1,] 24
# Cross product dan fungsi diag()
crossprod(A, B) # Sama dengan t(A) %*% B
## [,1] [,2]
## [1,] 10 24
## [2,] 7 17
v <- c(1, 2, 3)
diag(v) # Matriks diagonal dari vektor v
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 2 0
## [3,] 0 0 3
diag(A) # Mengambil diagonal utama matriks A
## [1] 4 2
diag(3) # Matriks identitas 3x3
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
# Sistem Persamaan Linear dan Inversi (A %*% x = b)
b <- c(5, 7) # Variabel pendukung b
x_sol <- solve(A, b) # Solusi sistem linear
solve(A) # Invers dari matriks A
## [,1] [,2]
## [1,] -2 3
## [2,] 3 -4
# Bentuk kuadratik x^T %*% A^-1 %*% x
x %*% solve(A, x)
## [,1]
## [1,] -6
Sm <- matrix(c(2, 1, 1, 2), nrow = 2) # Matriks simetris Sm
ev <- eigen(Sm)
evals <- eigen(Sm)$values
evals_only <- eigen(Sm, only.values = TRUE)$values
M <- matrix(c(1, 2, 3, 4), nrow = 2) # Matriks kuadrat M
svd(M)
## $d
## [1] 5.4649857 0.3659662
##
## $u
## [,1] [,2]
## [1,] -0.5760484 -0.8174156
## [2,] -0.8174156 0.5760484
##
## $v
## [,1] [,2]
## [1,] -0.4045536 0.9145143
## [2,] -0.9145143 -0.4045536
absdetM <- prod(svd(M)$d)
# Fungsi buatan sendiri untuk absolute determinant
absdet <- function(M) prod(svd(M)$d)
# Determinant bawaan R
det(M)
## [1] -2
X <- cbind(1, 1:10) # Matriks desain X
y <- 2 + 3 * (1:10) + rnorm(10) # Vektor observasi y
ans <- lsfit(X, y) # Least squares fit
## Warning in lsfit(X, y): 'X' matrix was collinear
# Menggunakan Decomposition QR
Xplus <- qr(X)
b <- qr.coef(Xplus, y)
fit <- qr.fitted(Xplus, y)
res <- qr.resid(Xplus, y)
X1 <- matrix(1:6, nrow = 3)
X2 <- matrix(7:12, nrow = 3)
X <- cbind(1, X1, X2) # Menggabungkan kolom dengan kolom 1 ber-isi angka 1
vec <- as.vector(X) # Mengubah matriks/array kembali menjadi vektor
vec2 <- c(X) # Cara alternatif menggunakan c()
# Menyiapkan data contoh untuk statef dan incomes
statef <- factor(c("act", "nsw", "nt", "qld", "sa", "tas", "vic", "wa", "nsw", "vic"))
incomes <- c(38, 42, 51, 63, 47, 58, 67, 49, 52, 71)
# Tabel frekuensi satu arah
statefr <- table(statef)
statefr_alt <- tapply(statef, statef, length)
# Membuat faktor kelompok pendapatan dengan cut()
incomef <- factor(cut(incomes, breaks = 35 + 10 * (0:7)))
# Tabel frekuensi dua arah (Two-way frequency table)
table(incomef, statef)
## statef
## incomef act nsw nt qld sa tas vic wa
## (35,45] 1 1 0 0 0 0 0 0
## (45,55] 0 1 1 0 1 0 0 1
## (55,65] 0 0 0 1 0 1 0 0
## (65,75] 0 0 0 0 0 0 2 0