Intermediate R: Advanced data wrangling, functions, and control flow

Session 7

September 26, 2023

Learning objectives

  • Write a data wrangling script using tidy verbs
  • Convert a tailor-made script to a function
  • Use if-else logic to generalize functions

Part I: Data wrangling refresher

Picking up from last week

  • Commit last week’s work to GitHub and push
  • Merge your work to main!
  • Create a new branch for today’s work and pull

Data wrangling objectives:

  • Write a data wrangling script using the functions we covered last week
    • Use mutate to convert units of columns
    • Use summarize to calculate group-wise summary statistics
    • Use pivot_wider to create cross-tabs
  • Use “tidy selection” helpers to reduce repetition in this workflow!

Part II: Functions

What are functions in R?

  • We use pre-written functions constantly.
  • Automated “recipes” for executing code
  • Inputs (“arguments”) and outputs

Why write our own functions?

  • Avoid repetition (+ mistakes)
  • Enhance portability + reproducibility

Writing a function objectives

  • Convert our data wrangling script into a function
  • Break this function into smaller, human-readable functions
  • Make this function flexible to apply to other data

Anatomy of a function

# Defining a function
# a_function will be the name of the function
# arguments go within the ()
# code goes in the curly braces

a_function <- function(an_argument) {
  # code to execute goes here
  
  # whatever is inside return() comes out of the funciton
  return(final_output) 
}

# Run the code to create the function
# Then you can use it:
# a_function(<some_input>)
# 

Part III: Conditionals

What are conditionals?

  • if and ifelse statements
  • Balance readability and flexibility in functions

Conditionals in R

  • == for “is equal to”
  • != for “not equal to:
  • <, > for less than/greater than
  • <=, >= for less than or equal to/greater than or equal to
  • is.na() for is it NA?
  • %in% for, is this value in a vector of possibilities
  • …and more!

Conditionals objectives

  • Add ifelse logic to our data wrangling function

Finishing up

  • Commit your changes and push
  • Open a pull request.

Homework

  • None, except for…

Show and tell (Late October)

  • An opportunity to apply workshop skills to your own work.
  • You could apply different parts to different projects, or more integratively to one single project.
  • Optional, but encouraged!

Some inspirations

  • Turn the project folder for one of your current research project into a git/GitHub repo and/or R project
  • Modify an R script to use functions, conditionals, or best-practices for code formatting
  • Improve directory organization for project or your entire computer!