# 1.1 Menampilkan daftar objek di workspace saat ini
objects()
#> character(0)
ls()
#> character(0)
# 1.2 Membuat objek uji coba
temp_a <- 10
temp_b <- 25

# 1.3 Menghapus objek tertentu dari workspace
rm(temp_a, temp_b)

# 1.4 Verifikasi pembersihan
ls()
#> character(0)
# 2.1 Vektor dan Assignment
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
1 / x
#> [1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295
y <- c(x, 0, x)
y
#>  [1] 10.4  5.6  3.1  6.4 21.7  0.0 10.4  5.6  3.1  6.4 21.7
# 2.2 Vector Arithmetic & Statistik Deskriptif Dasar
v <- 2 * x + y[1:5] + 1
v
#> [1] 32.2 17.8 10.3 20.2 66.1
mean(x)
#> [1] 9.44
var(x)
#> [1] 53.853
sort(x)
#> [1]  3.1  5.6  6.4 10.4 21.7
# 2.3 Regular Sequences & Replication
s1 <- 1:30
s2 <- seq(from = 1, to = 30, by = 2)
s3 <- seq(-5, 5, by = 0.2)
s4 <- rep(x, times = 2)
s5 <- rep(x, each = 2)

# 2.4 Logical Vectors
temp <- x > 13
temp
#> [1] FALSE FALSE FALSE FALSE  TRUE
# 2.5 Missing Values (NA dan NaN)
z_na <- c(1:3, NA)
is.na(z_na)
#> [1] FALSE FALSE FALSE  TRUE
0 / 0          # Menghasilkan NaN
#> [1] NaN
is.nan(0 / 0)
#> [1] TRUE
# 2.6 Character Vectors & Paste
labs <- paste(c("X", "Y"), 1:5, sep = "")
labs
#> [1] "X1" "Y2" "X3" "Y4" "X5"
# 2.7 Indexing & Subsetting
y_clean <- x[!is.na(x)]       # Ambil yang bukan NA
x_pos <- (x + 1)[x > 5]       # Ambil elemen > 5
x_pos
#> [1] 11.4  6.6  7.4 22.7
x[1:3]                        # Ambil elemen ke 1 sampai 3
#> [1] 10.4  5.6  3.1
x[-(1:2)]                     # Hilangkan elemen ke 1 dan 2
#> [1]  3.1  6.4 21.7
# 3.1 Mode dan Length
mode(x)
#> [1] "numeric"
length(x)
#> [1] 5
# Coercion (Perubahan Mode)
z_num <- 0:9
digits <- as.character(z_num)
digits
#>  [1] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
mode(digits)
#> [1] "character"
d_back <- as.integer(digits)

# 3.2 Mengubah Ukuran Objek (Dynamically Resizing)
e <- numeric()
e[3] <- 17
e
#> [1] NA NA 17
# 3.3 Attributes dan Dimensi
z_mat <- 1:12
attr(z_mat, "dim") <- c(3, 4)
z_mat
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12
# 3.4 Kelas Objek (Class)
class(z_mat)
#> [1] "matrix" "array"
unclass(z_mat)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12
# 4.1 Membuat Vektor Kategori (State) dan Faktor (tepat 30 elemen)
state <- c("tas", "sa",  "qld", "nsw", "nsw", "nt",  "wa",  "wa",
           "qld", "sa",  "vic", "nsw", "vic", "qld", "qld", "sa",
           "tas", "sa",  "nt",  "wa",  "vic", "qld", "nsw", "nsw",
           "wa",  "sa",  "act", "nsw", "vic", "vic")

statef <- factor(state)
levels(statef)
#> [1] "act" "nsw" "nt"  "qld" "sa"  "tas" "vic" "wa"
# 4.2 Ragged Arrays & Fungsi tapply()
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)

# Rata-rata pendapatan per state
incmeans <- tapply(incomes, statef, mean)
incmeans
#>      act      nsw       nt      qld       sa      tas      vic       wa 
#> 59.00000 50.33333 54.00000 54.60000 55.20000 59.00000 55.20000 57.50000
# Menghitung Standard Error kustom dengan function
stdError <- function(x) sqrt(var(x) / length(x))
incster <- tapply(incomes, statef, stdError)
incster
#>      act      nsw       nt      qld       sa      tas      vic       wa 
#>       NA 4.005552 6.000000 4.365776 4.270831 1.000000 4.554119 2.901149
# 4.3 Ordered Factors
income_levels <- ordered(c("Low", "Medium", "High", "Medium", "Low"), 
                         levels = c("Low", "Medium", "High"))
income_levels
#> [1] Low    Medium High   Medium Low   
#> Levels: Low < Medium < High
# 5.1 Membuat Matriks & Array
A <- matrix(1:12, nrow = 3, ncol = 4)
A
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12
# 5.2 & 5.3 Index Matrix (Mengambil & Mengubah elemen tertentu)
x_arr <- array(1:20, dim = c(4, 5))
x_arr
#>      [,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
idx <- matrix(c(1, 3, 2, 2, 3, 1), ncol = 2, byrow = TRUE)
x_arr[idx]        # Ekstrak elemen spesifik
#> [1] 9 6 3
x_arr[idx] <- 0   # Ubah elemen yang dipilih jadi 0
x_arr
#>      [,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
# 5.5 Outer Product
outer_prod <- outer(1:3, 1:4, "*")
outer_prod
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    2    4    6    8
#> [3,]    3    6    9   12
# 5.7 Operasi Aljabar Linear Matriks
M <- matrix(c(2, 1, 1, 3), nrow = 2)
b <- c(8, 13)

# Perkalian Matriks (%*%)
M %*% b
#>      [,1]
#> [1,]   29
#> [2,]   47
# Menyelesaikan Sistem Persamaan Linear (M x = b -> x = solve(M, b))
solusi_x <- solve(M, b)
solusi_x
#> [1] 2.2 3.6
# Invers Matriks & Determinan
solve(M)
#>      [,1] [,2]
#> [1,]  0.6 -0.2
#> [2,] -0.2  0.4
det(M)
#> [1] 5
# Eigenvalues & Eigenvectors
eig <- eigen(M)
eig$values
#> [1] 3.618034 1.381966
eig$vectors
#>           [,1]       [,2]
#> [1,] 0.5257311 -0.8506508
#> [2,] 0.8506508  0.5257311
# Singular Value Decomposition (SVD)
svd_res <- svd(M)
svd_res$d
#> [1] 3.618034 1.381966
# 5.8 Menggabungkan Matriks (cbind & rbind)
kolom_gabung <- cbind(M, c(5, 7))
baris_gabung <- rbind(M, c(9, 9))
kolom_gabung
#>      [,1] [,2] [,3]
#> [1,]    2    1    5
#> [2,]    1    3    7
baris_gabung
#>      [,1] [,2]
#> [1,]    2    1
#> [2,]    1    3
#> [3,]    9    9
# 5.10 Tabel Kontingensi (Frequency Table)
table(statef)
#> statef
#> act nsw  nt qld  sa tas vic  wa 
#>   1   6   2   5   5   2   5   4