Week 03: Program

[03a] Assignment / Functions

Topics

  • Store reusable data in named objects using assignment (<-)
  • Transform data and objects using one or more functions

Readings

Slides

*Note that you can click the three-line (hamburger) icon on the bottom-left of the slides to access a navigation menu. You can click inside the slides region and press the left and right arrow keys on your keyboard to advance and reverse the slides and animations.

Practice

1) Assignment

Which of the following commands do you think will create an error in R? Why?

  1. score@T1 <- 3.2
  2. score_at_T1 <- 3.2
  3. score at T1 <- 3.2
  4. 1_score <- 3.2
  5. ScoreAtTime1 <- 3.2

Make your best guesses just by looking at the commands and then check your guesses by running the commands in R. Bonus: Can you come up with a better name for this variable?

Answer key

Code Result (reason)
score@T1 <- 3.2 🛑 Error (@ not allowed in name)
score_at_T1 <- 3.2 ✅ No Error
score at T1 <- 3.2 🛑 Error (spaces not allowed in name)
1_score <- 3.2 🛑 Error (name can’t start with a number)
ScoreAtTime1 <- 3.2 ✅ No Error

2) Functions

What do you expect the result to be for the following commands?

  1. sqrt(4 + 5)
  2. sqrt(4) + 5
  3. sqrt(1 + sqrt(9))

Make your best guesses just by looking at the commands and then check your guesses by running the commands in R.

Answer key

# Part (a): Add then root, same as sqrt(9)
sqrt(4 + 5)
## [1] 3

# Part (b): Root then add, same as 2 + 5
sqrt(4) + 5 
## [1] 7

# Part (c): Inner root then add then outer root, same as sqrt(1 + 3)
sqrt(1 + sqrt(9))
## [1] 2

[03b] Arguments / Vectors

Topics

  • Customize the behavior of functions by modifying their arguments
  • Collect multiple objects into vectors using the c() function

Readings

Slides

*Note that you can click the three-line (hamburger) icon on the bottom-left of the slides to access a navigation menu. You can click inside the slides region and press the left and right arrow keys on your keyboard to advance and reverse the slides and animations.

Practice

1) Arguments

  1. Use the cos() function to calculate the cosine of 3 and assign it to a variable called y. Print the value of y to the console.
  2. Use a function to round y to the nearest whole number.
  3. Use a function to round y to two decimal places.

Answer key

# Part (a)
y <- cos(3)
y
## [1] -0.9899925

# Part (b)
round(y)
## [1] -1

# Part (c)
round(y, digits = 2)
## [1] -0.99

2) Vectors

Imagine that you ran a stand selling bananas for three days. On the first day, you sold $30 worth of bananas and spent $15 on supplies. On the second day, you sold $50 worth of bananas and spent $15 on supplies. On the third day, you sold $40 worth of bananas and spent $100 on repairs to the stand (due to an unfortunate fire).

  1. Create two vectors named sales and costs to store how much you sold and how much you spent on each day, respectively.

  2. Subtract the costs object from the sales object and save the result to a new object named profits. Print this to see your profits per day.

  3. Use the sum() function to calculate your total profits over all three days. Was your effort fruitful, i.e., did you make money overall?

Answer key

# Part (a)
sales <- c(30, 50, 40)
costs <- c(15, 15, 100)

# Part (b)
profits <- sales - costs
profits
## [1]  15  35 -60

# Part (c): No, I lost money!
sum(profits)
## [1] -10

[03c] Strings / Packages

Topics

  • Store text/character data in strings and character vectors
  • Install, load, and use data and functions from R packages

Readings

Slides

*Note that you can click the three-line (hamburger) icon on the bottom-left of the slides to access a navigation menu. You can click inside the slides region and press the left and right arrow keys on your keyboard to advance and reverse the slides and animations.

Practice

1) Strings

  1. Create a character vector named flavors that contains the following strings and print it to the console:
  • Cookies & Cream
  • Americone Dream (R)
  • Bob Marley’s 1 Love
  1. Use a function to calculate the number of strings in flavors.
  2. Use another function to calculate the number of characters in each string in flavors.
  3. Let R know how you feel about ice cream by using a function to either make all the characters in flavors uppercase (you LOVE ice cream) or lowercase (you don’t love ice cream).

Answer key

# Part (a)
flavors <- c("Cookies & Cream", "Americone Dream (R)", "Bob Marley's 1 Love")
flavors
## [1] "Cookies & Cream"     "Americone Dream (R)" "Bob Marley's 1 Love"

# Part (b)
length(flavors)
## [1] 3

# Part (c)
nchar(flavors)
## [1] 15 19 19

# Part (d)
toupper(flavors)
## [1] "COOKIES & CREAM"     "AMERICONE DREAM (R)" "BOB MARLEY'S 1 LOVE"
tolower(flavors)
## [1] "cookies & cream"     "americone dream (r)" "bob marley's 1 love"

2) Packages

  1. Install the tidyverse package. It may take several minutes to install because it contains several sub-packages that we will use in this class.
  2. Use a function we discussed in lecture to view the vignettes included in the tidyverse package (hint: be careful about uppercase and lowercase letters in the name of the function).
  3. In the new window that opens, click the link that says “HTML” next to “Welcome to the tidyverse” and read that vignette (it is relatively brief and will prepare you for next week).

Answer key

# Part (a)
install.packages("tidyverse") # in console not in Quarto
# or RStudio > Packages tab > Install button

# Part (b)
browseVignettes("tidyverse") # in console not in Quarto

# Part (c)

https://tidyverse.tidyverse.org/articles/paper.html