Vectorized functions
Learning Objectives
This week, students will:
- Learn to run functions on multiple datasets by using:
- vectorized functions
apply
functions- the function
rowwise()
fromdplyr
- Understand that the
group_by()
function fromdplyr
runs the same function on multiple data sets- Learn the difference between these four methods for code reusing
- Manipulate character strings with the package
stringr
andpaste()
Practice Objectives
- Creating functions
- Deafult values for arguments
- Reading tables
- Using functions from
dplyr
andggplot2
Setup your RStudio project (5 min)
- 🎗️ Structuring your files into a project is a best practice for good data science!
- Open your RStudio project for the class; I called mine “fall-2022”.
- Open a new file, name it “apply.Rmd”, and save it to your “documents” folder.
Vectorized functions (5 min)
Exercise 1: Calculating the mass of a bunch of dinosaurs (35 min)
sapply()
(5 min)
Exercise 2: Conditioning on length (15 min)
mapply()
(5 min)
Exercise 3: A data set of dinosaur lengths (30 min)
Discussion (20 min)
- How does using the functions you practiced today (vectorized and
apply
functions) is different from creating your own (regular) functions?- Creating and using a regular function:
- avoids repeating a group of code
- can be applied to a small number of values in a given amount of time
- Using vectorized functions and apply functions:
- avoids repeating the same funcion over and over with different values
- can be applied to an almost infinite number of values in seconds
- Creating and using a regular function: