Week 2

Fundamentals of R

Monday, Jan 13

Learning objectives

  • Vectors as building blocks
  • Vectorization and recycling
  • Logical and comparison operators
  • Conditional control flow
  • Iteration with for, while, and repeat

Materials


Data types and functions

Wednesday, Jan 15

Learning objectives

  • Atomic vector manipulation
  • Creating generic vectors
  • Function writing best practices
  • Four function forms
  • Function scope and lazy evaluation
  • Handling reserved words: Inf, NaN, NA

Materials


Lab 2

Friday, Jan 17

GitHub

Learning objectives

  • Function writing
  • Control flow
  • R code style
  • R Markdown
    • Code chunk options
    • Writing narrative text and code
    • Knitting
    • Markdown syntax

Materials


Exercise of the week

Write a function called seq_fib(). Function seq_fib() should take one argument, n - an atomic numeric vector of length 1, and return the first n Fibonacci numbers as an atomic vector. Some examples of the function in action are given below.

Valid inputs

seq_fib(n = 1)
seq_fib(n = 2)
seq_fib(n = 7)
seq_fib(n = 10)
## [1] 0
## [1] 0 1
## [1] 0 1 1 2 3 5 8
##  [1]  0  1  1  2  3  5  8 13 21 34

Invalid inputs

seq_fib(n = -2)
## Error in seq_fib(n = -2): n >= 1 is not TRUE
seq_fib(n = c(2, 3, 10))
## Error in seq_fib(n = c(2, 3, 10)): length(n) == 1 is not TRUE

Did you remember to handle situations such as seq_fib(n = 4.9)?

Previous
Next