Introduction and Preliminaries

SIMPLE MANIPULATIONS; NUMBER AND VECTORS

VEKTOR N ASSIGNMENT

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)

VECTOR ARITHMETIC

v <- 2*x + y + 1
## Warning in 2 * x + y: longer object length is not a multiple of shorter object
## length
# panjang x dan y tidak sama, cek
length(x)
## [1] 5
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

GENERATING REGULAR SEQUENCES

seq(-5, 5, by=.2)-> s3
s4 <- seq(length=51, from=-5, by=.2)
s5 <- rep(x, times=5)
s6 <- rep(x, each=5)

LOGICAL VECTORS

temp <- x > 13

MISS VALUES

z <- c(1:3,NA); ind <- is.na(z)
0/0
## [1] NaN
Inf-Inf
## [1] NaN

CHARACTER VECTORS

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"

INDEX VECTORS

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)

3 OBJECTS, THEIR MODES AND ATRRIBUTES

INTRINSIC ATRRIBUTES

z <- 0:9
digits <- as.character(z)
d <- as.integer(digits)

CHANGING THE LENGTH OF AN OBJECT

e <- numeric()
e[3] <- 17
alpha <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
alpha <- alpha[2 * 1:5]
length(alpha) <- 3

GETTING AND SETTING ATTRIBUTES

attr(z, "dim") <- c(2,5)

THE CLASS OF AN OBJECT

winter <- factor(c("dingin", "salju", "dingin"))
unclass(winter)
## [1] 1 2 1
## attr(,"levels")
## [1] "dingin" "salju"

4 ORDERED AND UNORDERED FACTORS

A SPECIFIC EXAMPLE

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"

FUNCTION TAPPLY () AND RAGGED ARRAYS

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

5 ARRAYS AND MATRICES

ARRAYS

dim(z) <- c(2, 5, 1)
x <- array(1:20, dim=c(4,5))
i <- array(c(1:3, 3:1), dim=c(3,2))
x[i]           # Memanggil elemen x berdasarkan indeks matriks i
## [1] 9 6 3
x[i] <- 0      # Mengubah elemen tersebut menjadi 0
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

MATRIKS DESAIN

n <- 6; b <- 2; v <- 3
blocks <- c(1, 1, 1, 2, 2, 2)
varieties <- c(1, 2, 3, 2, 3, 1)
Xb <- matrix(0, n, b)
Xv <- matrix(0, n, v)
ib <- cbind(1:n, blocks)
iv <- cbind(1:n, varieties)
Xb[ib] <- 1
Xv[iv] <- 1
X <- cbind(Xb, Xv)
N <- crossprod(Xb, Xv)
N <- table(blocks, varieties)
data_vector <- 1:24
dim_vector <- c(3,4,2)
Z <- array(data_vector, dim_vector)
h <- 1:24
Z_baru <- array(h, dim=c(3,4,2))
dim(h) <- c(3,4,2)
Z_nol <- array(0, c(3,4,2))
# Mendefinisikan A, B, dan C sebagai array berukuran sama
A_arr <- array(1:8, dim=c(2,2,2))
B_arr <- array(8:1, dim=c(2,2,2))
C_arr <- array(1, dim=c(2,2,2))
D <- 2 * A_arr * B_arr + C_arr + 1
a <- 1:3
b <- 4:6
ab <- a %o% b
ab_alt <- outer(a, b, "*")
# Memasukkan fungsi matematika ke dalam outer
f <- function(x, y) cos(y)/(1+x^2)
x_vec <- seq(-2, 2, length=5)
y_vec <- seq(-pi, pi, length=5)
z_outer <- outer(x_vec, y_vec, f)
# Menghitung determinan acak dan membuat plot
d <- outer(0:9, 0:9)
fr <- table(outer(d, d, "-"))
plot(fr, xlab="Determinant", ylab="Frequency")