Function in R
f <- function() {
## This is an empty function
}
# sum of two numbers
SUM <- function(a,b){
a + b
}
SUM(3,4)
## [1] 7
SUM(1.4, 5.6)
## [1] 7
Control Structures 1: if-else
if and else: testing a condition and acting
on it
# Parity of an integer
parity <- function(n){
if(n %% 2 == 0){"even"}else{"odd"} # i think %% corresponds to divisible by 2, =0 means no remainders or anything
}
parity(0)
## [1] "even"
parity(35)
## [1] "odd"
parity(-12)
## [1] "even"
parity(3.5)
## [1] "odd"
# Parity of an integer
parity <- function(n){
if(is.numeric(n)){
m <- as.integer(n)
if(m %% 2 == 0){
paste(m, "is even.")
}else{
paste(m, "is odd")
}
}else{
paste("Warning :",n,"is not a number.")
}
}
parity(12)
## [1] "12 is even."
parity(5.6) #mula numeric ginwa niyang integer nice
## [1] "5 is odd"
parity("abc")
## [1] "Warning : abc is not a number."
PAR <- function(n){
ifelse(n %% 2 == 0, paste(n, "is even"), paste(n, "is odd")) #pinagsama na yung if and else so ifelse
}
PAR(21)
## [1] "21 is odd"
Control Structure 2: for Loops
for: execute a loop a fixed number of times
x <- "Ateneo de Manila"
n <- nchar(x) # count the number of characters
for(i in 1:n){
print(substr(x, start = i,stop = i))
}
## [1] "A"
## [1] "t"
## [1] "e"
## [1] "n"
## [1] "e"
## [1] "o"
## [1] " "
## [1] "d"
## [1] "e"
## [1] " "
## [1] "M"
## [1] "a"
## [1] "n"
## [1] "i"
## [1] "l"
## [1] "a"
for(i in 1:n){
print(substr(x, start = 1,stop = i))
}
## [1] "A"
## [1] "At"
## [1] "Ate"
## [1] "Aten"
## [1] "Atene"
## [1] "Ateneo"
## [1] "Ateneo "
## [1] "Ateneo d"
## [1] "Ateneo de"
## [1] "Ateneo de "
## [1] "Ateneo de M"
## [1] "Ateneo de Ma"
## [1] "Ateneo de Man"
## [1] "Ateneo de Mani"
## [1] "Ateneo de Manil"
## [1] "Ateneo de Manila"
for(i in seq_along(LETTERS)){
print(LETTERS[i])
}
## [1] "A"
## [1] "B"
## [1] "C"
## [1] "D"
## [1] "E"
## [1] "F"
## [1] "G"
## [1] "H"
## [1] "I"
## [1] "J"
## [1] "K"
## [1] "L"
## [1] "M"
## [1] "N"
## [1] "O"
## [1] "P"
## [1] "Q"
## [1] "R"
## [1] "S"
## [1] "T"
## [1] "U"
## [1] "V"
## [1] "W"
## [1] "X"
## [1] "Y"
## [1] "Z"
Control Structure 2: Nested for loops
for loops can be nested inside of each other.
x <- matrix(1:6, nrow = 3)
x
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
for(i in seq_len(nrow(x))){
for(j in seq_len(ncol(x))){
#print(paste0("x[",i, ", ",j, "] = ", x[i,j]))
print(sprintf("x[%i, %i] = %i", i, j, x[i, j]))
}
}
## [1] "x[1, 1] = 1"
## [1] "x[1, 2] = 4"
## [1] "x[2, 1] = 2"
## [1] "x[2, 2] = 5"
## [1] "x[3, 1] = 3"
## [1] "x[3, 2] = 6"
Control Structure 3: Nested while loops
# Division Algorithm
# Given two integers a and m where m > 0
# Find the quotient and the remainder
quorem <- function(a,m){
q = 0
r = a - m * q
## a = mq + r where r = 0, 1, ..., m-1
print(sprintf("%i = %i * %i + %i", a, m, q, r))
if (m > 0){
while(r >= m | r < 0){
if(a > 0){
q = q + 1
} else {
q = q - 1
}
r = a - m * q
print(sprintf("%i = %i * %i + %i", a, m, q, r))
}
print(sprintf("quotient = %i, remainder = %i", q, r))
} else {
paste("WARNING:", m, "must be positive.")
}
}
quorem(-12,5)
## [1] "-12 = 5 * 0 + -12"
## [1] "-12 = 5 * -1 + -7"
## [1] "-12 = 5 * -2 + -2"
## [1] "-12 = 5 * -3 + 3"
## [1] "quotient = -3, remainder = 3"
quorem(12, 2)
## [1] "12 = 2 * 0 + 12"
## [1] "12 = 2 * 1 + 10"
## [1] "12 = 2 * 2 + 8"
## [1] "12 = 2 * 3 + 6"
## [1] "12 = 2 * 4 + 4"
## [1] "12 = 2 * 5 + 2"
## [1] "12 = 2 * 6 + 0"
## [1] "quotient = 6, remainder = 0"
quorem(0,5)
## [1] "0 = 5 * 0 + 0"
## [1] "quotient = 0, remainder = 0"
quorem(100,10)
## [1] "100 = 10 * 0 + 100"
## [1] "100 = 10 * 1 + 90"
## [1] "100 = 10 * 2 + 80"
## [1] "100 = 10 * 3 + 70"
## [1] "100 = 10 * 4 + 60"
## [1] "100 = 10 * 5 + 50"
## [1] "100 = 10 * 6 + 40"
## [1] "100 = 10 * 7 + 30"
## [1] "100 = 10 * 8 + 20"
## [1] "100 = 10 * 9 + 10"
## [1] "100 = 10 * 10 + 0"
## [1] "quotient = 10, remainder = 0"
divmod <- function(a,m){
q = 0
r = a - m * q
while(r >= m | r < 0){
if(a > 0){
q = q + 1
} else {
q = q - 1
}
r = a - m * q
}
return(c(q,r))
}
divmod(12,5)
## [1] 2 2
# Euclidean Algorithm
GCD <- function(a, b){
x <- a
y <- b
while(y != 0){
r <- x %% y
q <- x %/% y # floor(x/y)
print(sprintf("%i = %i * %i + %i", x, y, q, r))
x <- y
y <- r
}
sprintf("GCD(%i, %i) = %i",a, b, x)
}
GCD(12, 8)
## [1] "12 = 8 * 1 + 4"
## [1] "8 = 4 * 2 + 0"
## [1] "GCD(12, 8) = 4"
GCD(5, 0)
## [1] "GCD(5, 0) = 5"
GCD(0, 5)
## [1] "0 = 5 * 0 + 0"
## [1] "GCD(0, 5) = 5"
GCD(100, 31)
## [1] "100 = 31 * 3 + 7"
## [1] "31 = 7 * 4 + 3"
## [1] "7 = 3 * 2 + 1"
## [1] "3 = 1 * 3 + 0"
## [1] "GCD(100, 31) = 1"
# Euclidean Algorithm
GCD_v2 <- function(a, b){
x <- a
y <- b
while(y != 0){
qr <- divmod(x,y)
print(sprintf("%i = %i * %i + %i", x, y, qr[1], qr[2]))
x <- y
y <- qr[2]
}
sprintf("GCD(%i, %i) = %i",a, b, x)
}
GCD_v2(12,8)
## [1] "12 = 8 * 1 + 4"
## [1] "8 = 4 * 2 + 0"
## [1] "GCD(12, 8) = 4"
Control Structure 4: Nested while repeat loops
squareRoot <- function(a,x0){
return(0.5 * (x0 + a/x0))
}
x0 <- 6
tol <- 1e-8
ctr <- 0
repeat {
x1 <- squareRoot(101, x0)
ctr <- ctr + 1
if(abs(x1 - x0) < tol | ctr > 100){
break
} else {
x0 <- x1
}
}
x1
## [1] 10.04988
GCD(321,54321)
## [1] "321 = 54321 * 0 + 321"
## [1] "54321 = 321 * 169 + 72"
## [1] "321 = 72 * 4 + 33"
## [1] "72 = 33 * 2 + 6"
## [1] "33 = 6 * 5 + 3"
## [1] "6 = 3 * 2 + 0"
## [1] "GCD(321, 54321) = 3"