Bab Iii, Iv, V Fix
Bab Iii, Iv, V Fix
Bab Iii, Iv, V Fix
PEMROGRAMAN R STUDIO
NAMA :
Hasil :
Hasil :
Hasil :
3.3.3 Example: pension value pension.r
Scripts :
Inputs
r <- 0.11
term <- 10
period <- 1/12
payments <- 100
n <- floor(term/period)
pension <- 0
for (i in 1:n) { pension[i+1] <- pension[i]*(1 + r*period) + payments }
time <- (0:n)*period
Output
plot(time, pension)
Hasil :
Hasil :
3.6 Program flow
Scripts :
x <- 3
for (i in 1:3)
{ show(x)
if (x %% 2 == 0)
{ x <- x/2 }
else
{ x <- 3*x + 1}}
show(x)
Hasil :
rm(list=ls())
data1= "C:/Users/ACER/Downloads/Metnum/data1.r"
data <- scan(file = data1)
n <- length(data)
data.sort <- sort(data)
data.1qrt <- data.sort[ceiling(n/4)]
data.med <- data.sort[ceiling(n/2)]
data.3qrt <- data.sort[ceiling(3*n/4)]
cat("1st Quartile:", data.1qrt, "\n")
cat("Median: ", data.med, "\n")
cat("3rd Quartile:", data.3qrt, "\n")
Hasil :
Scripts :
quantile(scan("C:/Users/ACER/Downloads/Metnum/data1.r"), (0:4)/4)
Hasil :
4.3 Input from the keyboard
Scripts :
rm(list=ls())
cat("find the zeros of a2*x^2 + a1*x + a0 = 0\n")
a2 <- as.numeric(readline("a2 = "))
a1 <- as.numeric(readline("a1 = "))
a0 <- as.numeric(readline("a0 = "))
discrim <- a1^2 - 4*a2*a0
if (discrim > 0) {
roots <- (-a1 + c(1,-1) * sqrt(a1^2 - 4*a2*a0))/(2*a2)
} else {
if (discrim == 0) {
roots <- -a1/(2*a2)
} else {
roots <- c()
}
}
if (length(roots) == 0) {
cat("no roots\n")
} else if (length(roots) == 1) {
cat("single root at", roots, "\n")
} else {
cat("roots at", roots[1], "and", roots[2], "\n")
}
source("quad2b.R")
Hasil :
4.5 Plotting
Scripts :
Hasil :
Scripts :
Hasil :
BAB V
PROGRAMMING WITH FUNCTIONS
5.1 Functions
5.1.1 Example: roots of a quadratic 3 quad3.r
Scripts :
# program spuRs/resources/scripts/quad3.r
quad3 <- function(a0, a1, a2) {
# find the zeros of a2*x^2 + a1*x + a0 = 0
if (a2 == 0 && a1 == 0 && a0 == 0) {
roots <- NA
} else if (a2 == 0 && a1 == 0) {
roots <- NULL
} else if (a2 == 0) {
roots <- -a0/a1
} else {
# calculate the discriminant
discrim <- a1^2 - 4*a2*a0
# calculate the roots depending on the value of the discriminant
if (discrim > 0) {
roots <- (-a1 + c(1,-1) * sqrt(a1^2 - 4*a2*a0))/(2*a2)
} else if (discrim == 0) {
roots <- -a1/(2*a2)
} else {
roots <- NULL
}
}
return(roots)
}
quad3(1,0,-1)
quad3(1,-2,1)
quad3(1,1,1)
Hasil :
5.1.2 Example: n choose r n_choose_r.r
Scripts :
# program spuRs/resources/scripts/n_choose_r.r
n_factorial <- function(n) {
# Calculate n factorial
n_fact <- prod(1:n)
return(n_fact)
}
n_choose_r <- function(n, r) {
# Calculate n choose r
n_ch_r <- n_factorial(n)/n_factorial(r)/n_factorial(n-r)
return(n_ch_r)
}
n_choose_r(4,2)
n_choose_r(6,4)
Hasil :
5.1.3 Example: Winsorised mean wmean.r
Scripts :
# program spuRs/resources/scripts/wmean.r
wmean <- function(x, k) {
# calculate the k-th Windsorised mean of the vector x
x <- c( 8.244, 51.421, 39.020, 90.574, 44.697,
+ 83.600, 73.760, 81.106, 38.811, 68.517)
n <- length(x)
x[1:k] <- x[k+1]
x[(n-k+1):n] <- x[n-k]
return(mean(x))
}
mean(x)
wmean(x, 2)
x.err <- x
x.err[1] <- 1000
mean(x.err)
wmean(x.err, 2)
Hasil :
5.1.4 Program flow using functions
Scripts :
# swap.r
swap <- function(x) {
# swap values of x[1] and x[2]
y <- x[2]
x[2] <- x[1]
x[1] <- y
return(x)
}
x <- c(7, 8, 9)
x[1:2] <- swap(x[1:2])
x[2:3] <- swap(x[2:3])
Hasil :
5.2 SCOPE AND ITS CONSEQUENCES
Scripts :
test <- function(x) {
y <- x + 1
return(y)
}
test(1)
x
y
y <- 10
test(1)
y
test2 <- function(x) {
y <- x + z
return(y)
}
z <- 1
test2(1)
z <- 2
test2(1)
Hasil :
5.3 ARGUMENTS
Scripts :
formals(test2)
test3 <- function(x = 1) {
return(x)
}
test3(2)
test3()
funk <- function(vibe = c("Do","Be","Dooby","Dooo")) {
vibe <- match.arg(vibe)
return(vibe)
}
funk()
funk("Dooby")
funk("Dum")
Hasil :
Scripts :
test4 <- function(x, ...) {
return(sd(x, ...))
}
test4(1:3)
test4(c(1:2,NA))
test4(c(1:2,NA), na.rm = TRUE)
test4(c(1:2,NA), TRUE)
Hasil :
Scripts :
test5 <- function(x = 1, y = 1, z = 1) {
return(x * 100 + y * 10 + z)
}
test5(2, 2)
test5(y = 2, z = 2)
test6 <- function(a = 1, b.c.d = 1) {
return(a + b.c.d)
}
test6()
test6(b = 5)
Hasil :
Scripts :
# program spuRs/resources/scripts/prime.r
prime <- function(n) {
# returns TRUE if n is prime
# assumes n is a positive integer
if (n == 1) {
is.prime <- FALSE
} else if (n == 2) {
is.prime <- TRUE
} else {
is.prime <- TRUE
m <- 2
m.max <- sqrt(n) # only want to calculate this once
while (is.prime && m <= m.max) {
if (n %% m == 0) is.prime <- FALSE
m <- m + 1
}
}
return(is.prime)
}
Hasil :