R Community of Practice

Week 1

Learning Goals

  1. import multiple files into RStudio at a time
  2. use tools for working with directories and file paths
  3. become familiar with concept of iteration
  4. understand how to work with a list structure

The Data

Scenario: we want to combine and analyze several spreadsheets containing workshop registration data. Each spreadsheet has the same structure - name of attendee 1, school affiliation, and status (faculty, student, staff).


data
├── Workshop_01.csv
├── Workshop_02.csv
├── Workshop_03.csv
├── Workshop_04.csv
├── Workshop_05.csv
├── Workshop_06.csv
├── Workshop_07.csv
├── Workshop_08.csv
├── Workshop_09.csv
└── Workshop_10.csv

The Power of Iteration

Loops

Loops are used in most programming languages when you want to repeat some set of code for multiple inputs.


for (thing in list_of_things) {
  do_some_function()
}

Functional Programming in R

In R, there are several functions that accomplish the same thing as loops, in particular:

  • The apply() family of functions in Base R
  • The map() family of functions from the purrr package in the tidyverse
map(.x, .f)

map(function-args, function)

Next Steps

So, we have our function read_csv(), and now we want it to repeat for all the file names in our directory. So - how do we accomplish this?

  1. Create a vector of file names
  2. Use map() to run read_csv() on each file name.
  3. Combine files into one data frame.
  4. Summarize data frame

Working with lists

  • Like vectors, lists are made up of elements, but those elements can be anything.
  • Often we use lists to work with several data frames at one time.
R Data Structures 1
homogeneous heterogenous
1d vector list
2d matrix data frame
nd array