ECON 413
R Functions

Erol Taymaz
Department of Economics
Middle East Technical University

Topics

R script

Creating functions

mySum <- function(a, b) {
    z <- a + b
    return(z)
}

mySum(3,4)
## [1] 7
# What happens to z?

# No explict return value
mySum <- function(a, b) {
    a + b
}

mySum(5,10)
## [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
mySum(5)
## [1] 6
mySum()
## [1] 1
mySum(b = 3, a = 1)
## [1] 4
# Setting default values for some arguments
mySum <- function(a, b = 1) {
    z <- a + b
    return(z)
}

mySum(5, 10)
## [1] 15
mySum(5)
## [1] 6
mySum()
## Error in mySum(): argument "a" is missing, with no default
mySum(b = 3, a = 1)
## [1] 4

Function components

mySum <- function(a = 0, b = 1) {
    z <- a + b
    return(z)
}


body(mySum)
## {
##     z <- a + b
##     return(z)
## }
formals(mySum)
## $a
## [1] 0
## 
## $b
## [1] 1
environment(mySum)
## <environment: R_GlobalEnv>
sumNA <- function(...) {
    return(sum(..., na.rm = TRUE))
}

body(sumNA)
## {
##     return(sum(..., na.rm = TRUE))
## }
formals(sumNA)
## $...
environment(sumNA)
## <environment: R_GlobalEnv>
aa <- c(1, 3, NA, 4)
sum(aa)
## [1] NA
sumNA(aa)
## [1] 8

Primitive functions

sum
## function (..., na.rm = FALSE)  .Primitive("sum")
body(sum)
## NULL
formals(sum)
## NULL
environment(sum)
## NULL

Name masking

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
f2()
## [1] 12
f3 <- function(a = 1) {
    b <<- a * 5
    d <<- 10
    e <- 20
}

a
## [1] 10
b
## [1] 100
# d
# e
f3()
a
## [1] 10
b
## [1] 5
d
## [1] 10
# e

Lazy evaluation

rm(list=ls())

f1 <- function(a = 10, b = d) {
  d <- (a + 1) ^ 2
  return(a + b)
}


f1()
## [1] 131
f1(1)
## [1] 5
f1(1, 5)
## [1] 6
f1 <- function(a = 1, b) {
  if (missing(b)) b = (a + 1) ^ 2
  return(a + b)
}

f1()
## [1] 5
f1(1)
## [1] 5
f1(1, 5)
## [1] 6
abss <- function(x) {
  if (x < 0) {
      x <- -1 * x
    }
    else {
      x <- x
    }
  return(x)
}

abss(-5)
## [1] 5
abss(1.5)
## [1] 1.5
# abss(c(-1, 0, 1))

absv <- function(x) {
  x[x < 0] <- -1 * x[x < 0]
  return(x)
}

absv(-5)
## [1] 5
absv(c(-1, 0, 1))
## [1] 1 0 1

Special functions

`%pow%` <- function(a, b) {a^b}

2 %pow% 3
## [1] 8
# Sum operator
`+`
## function (e1, e2)  .Primitive("+")
`+` <- function(a, b) {a - b}

2 + 2
## [1] 0
rm("+")

2 + 2
## [1] 4
`modify<-` <- function(x, position, value) {
  x[position] <- value
  x
}

x <- c(1:10)
x
##  [1]  1  2  3  4  5  6  7  8  9 10
modify(x, 1) <- 10
x
##  [1] 10  2  3  4  5  6  7  8  9 10

Debugging functions

deneme <- function(a, b) {
  d <- a + b
  e <- a * b
  f <- d + e
  return(f)
}

debug(deneme)
deneme(2, 3)

Loops in R

rm(list = ls())

a <- c(1:10)
b <- sample(10, 10)
a
b
d <- NULL

# for loop
for (i in c(1:10)) {
  d[i] <- a[i] + b[i]
}
d

# while loop
while ( i < 11) {
  d[i] <- a[i] * b[i]
  i <- i + 1
}
d

#  repeat loop
aa <- 1
while (aa != 0) {
 aa <- as.numeric(readline(prompt="Please, enter your a number: "))
 cat("Square of ", aa, " is equal to ", aa^2)
}

If possible, do not use loops!

aa <- sample(100, 1000000, replace = TRUE)
bb <- sample(100, 1000000, replace = TRUE)
sumAB <- 0

system.time(for (i in c(1:length(aa))) {
  sumAB <- sumAB + (aa[i] < bb[i])
  })
##    user  system elapsed 
##   0.139   0.012   0.150
sumAB
## [1] 495687
system.time(sumBA <- sum(aa < bb))
##    user  system elapsed 
##   0.003   0.000   0.003
sumBA
## [1] 495687