ECON 413
Algorithms, loops, functions
Erol
Taymaz
Department of Economics
Middle East Technical
University
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 4 1 10 9 3 2 7 5 8 6
## [1] 5 3 13 13 8 8 14 13 17 16
## [1] 4 2 30 36 15 12 49 40 72 60
## [1] 4 2 30 36 15 12 49 40 72 60
rm(list = ls())
set.seed(1234)
a <- c(1:10)
b <- sample(10, 10)
a
b
# next
d <- NULL
for (i in c(1:10)) {
if (1 == (i %% 2)) next
d[i] <- a[i] + b[i]
}
d
# break
d <- NULL
for (i in c(1:10)) {
if (i >= 5) break
d[i] <- a[i] + b[i]
}
d
# stop
d <- NULL
for (i in c(1:10)) {
if (i >= 5) stop("i should be less than 5")
d[i] <- a[i] + b[i]
}
d
d <- NULL
for (i in c(1:10)) {
if (i >= 5) warning("i should be less than 5")
d[i] <- a[i] + b[i]
}
d
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 10 6 5 4 1 8 2 7 9 3
## [1] FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE
d <- NULL
for (i in c(1:10)) {
if (a[i] > b[i]) {
d[i] <- 1
} else if (a[i] == b[i]) {
d[i] <- 0
} else {
d[i] <- -1
}
}
d
## [1] -1 -1 -1 0 1 -1 1 1 0 1
aa <- c(1:1000000)
bb <- c(1000000:1)
sumAB <- 0
sumBA <- 0
system.time(for (i in c(1:length(aa))) {
sumAB <- sumAB + (aa[i] < bb[i])
})
## user system elapsed
## 0.072 0.004 0.079
## [1] 5e+05
## user system elapsed
## 0.001 0.000 0.002
## [1] 500000
Task: Write program that finds the mean of a vector of values
Input: A numeric vector (V)
Output: The mean value (M)
Algorithm:
Algorithm
If the same code will be used many times, write a function
findMean <- function(V) {
T <- 0
N <- 1
repeat {
if (is.na(V[N])) break
T <- T + V[N]
N <- N + 1
}
M <- T / (N - 1)
return(M)
}
# Test examples
V <- 1
findMean(V)
V <- c(1:3)
findMean(V)
V <- c(1:10)
findMean(V)
V <- NULL
# findMean(V)
V <- c(1,NA,3)
findMean(V)
findMean2 <- function(V) {
T <- 0
N <- 1
for (i in c(1:length(V))) {
if (is.na(V[i])) next
T <- T + V[i]
N <- N + 1
}
M <- T / (N - 1)
return(M)
}
V <- c(1, NA, 3)
findMean2(V)
function_name (arg1, arg2, …) expression
sample(x, size, replace = FALSE, prob = NULL)
## [1] 3 3 3 2 3
## [1] 2 2 2 3 1
## [1] 2 2 1 2 3
## [1] 1 3
## [1] TRUE
## [1] TRUE
## [1] 4
## [1] 9
## [1] 11 12 13 14
## [1] 11 12 13 14
## [1] 5
## [1] 7
## [1] 15
# What if we assign the result to a variable?
mySum <- function(a, b) {
z <- a + b
}
mySum(5,10)
# Setting default values
mySum <- function(a = 0, b = 1) {
z <- a + b
return(z)
}
mySum(5, 10)
## [1] 15
## [1] 6
## [1] 1
## [1] 4
# Setting default values for some arguments
mySum <- function(a, b = 1) {
z <- a + b
return(z)
}
mySum(5, 10)
## [1] 15
## [1] 6
## Error in mySum(): argument "a" is missing, with no default
## [1] 4
## {
## z <- a + b
## return(z)
## }
## $a
## [1] 0
##
## $b
## [1] 1
## <environment: R_GlobalEnv>
## {
## return(sum(..., na.rm = TRUE))
## }
## $...
## <environment: R_GlobalEnv>
## [1] NA
## [1] 8
## function (..., na.rm = FALSE) .Primitive("sum")
## NULL
## NULL
## NULL
a <- 10
b <- 100
f1 <- function() {
a <- 1
b <- 2
return(sum(a + b))
}
f2 <- function() {
b <- 2
return(sum(a + b))
}
f1()
## [1] 3
## [1] 12
## [1] 10
## [1] 100
## [1] 10
## [1] 5
## [1] 10
## [1] 131
## [1] 5
## [1] 6
## [1] 5
## [1] 5
## [1] 6
## [1] 5
## [1] 1.5
## [1] 5
## [1] 1 0 1
## [1] 8
## function (e1, e2) .Primitive("+")
## [1] 0
## [1] 4
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 10 2 3 4 5 6 7 8 9 10