Bringing it all together

Using loops (for())

1 Overview & introduction

1.1 What you’ve learned about

  • R & RStudio
  • packages: tidyverse, knitr, kableExtra
  • data types
  • select(), filter(), |>, mutate()
  • factors

 

  • pivot_wider(), pivot_longer()
  • group_by(), summarize()
  • functions
  • Joins
  • Accessing IPEDS data

You have a real strong foundation for programming in R. We only have two more tools to discuss today.

1.2 Today

  1. The for() loop
    • Repeating queries
  2. Generating output for workbooks
  3. Extensive examples

We have just two topics to discuss today. However, most of your learning opportunities will come in the extensive lesson on loops and the homework.

Since today’s topic is “bringing it all together”, we’re going to do just that. We’re going to present three fairly extensive examples that will be somewhat daunting and also somewhat encouraging. You should use these as inspiration for what you will be able to grow in over the next few months of exploration and practice. You should also see some tools and tricks today that you should be able to incorporate into your project.

2 Loops: Using for()

As I’ve said, we’re going to see how to use the for() loop to repeat calculations and queries.

2.1 Structure of a for() loop

for (variable in vector) {
  statement(s)
}

It’s as simple as this. Instead of going through examples here in the slides, I’m going to go through them as a part of the university example that I’m going to discuss today.

Pay attention to:

  • source() as a tool for organizing your scripts
  • for() as a tool for looping
  • the functions for putting your output in worksheets

2.2 The university data model

3 Coming up

3.1 Dates

  • Bringing it all together
    • Monday, March 3: Week 4 (today)
    • Wednesday, March 5: Problem session (and projects!)
  • Week for you to work on your project
  • Project presentations
    • Monday, March 17
    • Plan to spend 5 minutes each discussing and showing (!) your project to the class
    • This will be our only to-do for the day

3.2 Work on the following

  1. Complete the Loops lesson.
  2. Read through the file university.R (and the associated source folder)
  3. Start working on the homework
  4. Do your course project
  5. Communicate with us if you have any questions about it

As for the homework:

  • Each one of the questions is a pretty reasonable model for a project.
  • The whole homework assignment might take you hours, days, or even weeks to complete…so don’t stress about completing it all. These are meant to be a resource that you can go back to over the next several months as your knowledge and skills grow.
  • But you can do them with the knowledge you’ve gained in this course!!