Foundations of
Data Science

Spring 2026 | Data 2 (399)
Jeffrey M. Girard | Lecture 03b

Roadmap

  1. Customize the behavior of functions by modifying their arguments

  2. Collect multiple objects into vectors using the c() function

Arguments

Arguments

  • Recipes allow chefs to cook up tasty treats
    • Recipes call for ingredients
    • Recipes involve one or more steps
    • Steps transform ingredients into treats
  • Functions are like customizable recipes
    • Functions call for inputs (“arguments”)
    • Functions involve one or more lines of code
    • Code transforms inputs into outputs
    • Put all arguments in the parentheses
    • Separate the arguments using commas

out <- f(in1, in2, in3)

Customized Rounding

# Default rounds to nearest even integer
round(2 / 3)
[1] 1
# But we can customize to round to 2 digits
round(2 / 3, digits = 2)
[1] 0.67
# Or to 3 digits
round(2 / 3, digits = 3)
[1] 0.667
# The "default" value is digits = 0
round(2 / 3, digits = 0)
[1] 1

Customized Logarithms

# Default is the natural logarithm (base = e)
log(10)
[1] 2.302585
# Verifying this using exp(1), which is e
log(10, base = exp(1))
[1] 2.302585
# Changing to base 2
log(10, base = 2)
[1] 3.321928
# Changing to base 10
log(10, base = 10)
[1] 1

Multiple Arguments

Randomly sample from the uniform distribution

# Generate n numbers between min and max, all equally likely
runif(n = 1, min = 10, max = 20)
[1] 15.98593

Randomly sample from the normal distribution

# Generate n numbers from a population with mean and sd
rnorm(n = 1, mean = 100, sd = 15)
[1] 118.5914

Vectors

Vectors

  • Vectors combine similar objects into a collection
    • I like to imagine a train pulling multiple cars
    • A vector is one object with many sub-objects
    • We refer to each sub-object as an element
  • Some functions transform each element in turn
    • Double the amount of cargo in every train car
  • Some functions summarize across elements
    • Calculate the total cargo across all train cars

v <- c(1, 2, 3)

Creating Simple Vectors

# We can't just type the elements out in a row
x <- 4 9 16 25 36
Error in parse(text = input): <text>:2:8: unexpected numeric constant
1: # We can't just type the elements out in a row
2: x <- 4 9
          ^
# Instead, we need to collect them using c()
x <- c(4, 9, 16, 25, 36)
x
[1]  4  9 16 25 36
# We can also append to a vector using c()
y <- c(x, 49, 64)
y
[1]  4  9 16 25 36 49 64

Generating Sequences

# You can use seq() to generate sequence vectors
x <- seq(from = 10, to = 20)
x
 [1] 10 11 12 13 14 15 16 17 18 19 20
# You can use by the control the spacing
y <- seq(from = 10, to = 20, by = 2)
y
[1] 10 12 14 16 18 20
# Sometimes length.out is more useful than by
z <- seq(from = 10, to = 20, length.out = 5)
z
[1] 10.0 12.5 15.0 17.5 20.0

Elementwise Operations

x <- c(10, 20, 30)
# Multiply every element in x by 3
y <- x * 3
y
[1] 30 60 90
# Subtract each element of y from the corresponding element in x
z <- x - y
z
[1] -20 -40 -60

Note that, for this to work, both vectors need the same number of elements

Elementwise Functions

x <- c(4, 9, 16, 25)
# Some functions also transform each element
sqrt(x)
[1] 2 3 4 5
# You can also combine operations and functions
log(x * 10)
[1] 3.688879 4.499810 5.075174 5.521461
# This can even apply with nested functions
round(log(x * 10))
[1] 4 4 5 6

Summary Functions

x <- c(10, 11, 12, 13, 14)
# The length of a vector is its number of elements
length(x)
[1] 5
# The sum of a vector adds all its elements together
sum(x)
[1] 60
# The mean of a vector divides the sum by the length
mean(x)
[1] 12

Others include: prod(), min(), max(), sd(), median(), and mad()