Foundations of
Data Science

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

Roadmap

  1. Store reusable data in named objects using assignment (<-)

  2. Transform data and objects using one or more functions

Assignment

Assignment

  • It is often useful to store data in named objects
    • This makes the data easier to use and re-use
    • This makes the code easier to write and read
  • Which command is easier to follow?
    1. Dial 7 8 5 8 6 4 0 8 4 1
    2. Call Office Phone
  • Named objects are created using assignment
    • Give a name then an arrow then the data

office <- 7858640841

Naming

  • Object names can only include:
    • Letters: a-Z
    • Numbers: 0-9
    • Underscores: _
    • Periods: .
  • Additional Rules:
    • Must start with a letter or period
    • Cannot contain spaces or dashes
    • Cannot contain other symbols
    • Names are case-sensitive (ageAge)

Basic Assignment

# x gets 2, print x
x <- 2
x
[1] 2
# Make y from x, print y
y <- 3 * x
y
[1] 6

Updating Objects

# Using x will not update it
x + 1
[1] 3
# x is still 2
x
[1] 2
# Use <- again to update x
x <- x + 1
x
[1] 3

Names are case-sensitive

# We already created lowercase y with assignment
y
[1] 6
# But we have not yet created uppercase Y
Y
Error:
! object 'Y' not found

Finding the right balance

# rate is allowed but pretty short
rate <- 93
rate
[1] 93
# heart_rate_in_beats_per_minute is allowed but pretty long
heart_rate_in_beats_per_minute <- 93
heart_rate_in_beats_per_minute
[1] 93
# heart_rate_bpm might be a nice compromise
heart_rate_bpm <- 93
heart_rate_bpm
[1] 93

Be careful of special symbols

# Don't try to add spaces
second age <- 12
Error in parse(text = input): <text>:2:8: unexpected symbol
1: # Don't try to add spaces
2: second age
          ^
# Don't try to add special symbols
age@time2 <- 12
Error:
! object 'age' not found
# Focus on letters, numbers, and underscores
age_time2 <- 12
age_time2
[1] 12

Functions

Functions

  • Functions are like calculator buttons
    • They let you easily apply a transformation
    • They can have descriptive names for readability
    • Is it obvious x^0.5 is the square root of x?
    • Is it obvious sqrt(x) is the square root of x?
  • We may want to combine multiple functions
    • We could use intermediate objects…
    • But “nested” functions take less room
    • We resolve nested functions inside-out
    • Using “pipes” is even better [04c]

Calling Functions

# Can I turn this into a "button" to press?
9^0.5
[1] 3
# Take the square root of a number
x <- sqrt(9)
x
[1] 3
# Or take the square root of an object
y <- sqrt(x)
y
[1] 1.732051

Rounding Functions

# Round up
ceiling(1 / 3)
[1] 1
# Round down
floor(1 / 3)
[1] 0
# Round to nearest even integer
round(0.5)
[1] 0
# Round to nearest even integer
round(1.5)
[1] 2

Other Useful Functions

# Absolute Value
abs(-3)
[1] 3
# Sign
sign(-3)
[1] -1
# Logarithm
log(1)
[1] 0
# Exponential
exp(1)
[1] 2.718282

Basic Trigonometry

# Sine
sin(1)
[1] 0.841471
# Cosine
cos(1)
[1] 0.5403023
# Tangent
tan(1)
[1] 1.557408

Note: there are corresponding asin(), acos(), and atan() functions.

Combining Functions

# Using intermediate objects works but takes many lines
x <- sqrt(9)
y <- cos(x)
z <- round(y)
z
[1] -1
# Alternatively, "nest" them and then resolve inside-out
z <- round(cos(sqrt(9)))
z
[1] -1