Lecture 2: Reading Data into R

DSCI 100

Housekeeping

  • Late Registrants

    • Welcome!

    • Any assignment from a class that happened before you registered is due 1 week after you registered

    • Any assignment from a class that happened after you registered is due at the regular time

Housekeeping

  • Communications:

    • Questions about course material/worksheets/clarification questions -> Piazza or Office Hours

    • Questions about academic consessions, due dates, late registration, admin stuff -> Julia (courses@stat.ubc.ca)

    • Private matters, accommodations, mentorship -> Email me or come by Office Hours


  • Office hours (see Canvas for schedule):
    • Please feel free to drop by ANY office hours regardless of the section that you are in

Worksheets/Tutorials

  • No need to submit anything on Canvas. Server will submit your work for you at the deadline!

  • Remember: when in doubt,

    • Kernel -> Restart Kernel and Run All (or the ▶▶ button)
  • When you are working on assignments there is a cell that says

DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER

make sure you double click and edit that cell directly. Your question won’t be graded otherwise.

  • Shut down server when you are done working

Worksheets/Tutorials - Week 1 and Week 2

  • No need to submit anything on Canvas. Server will submit your work for you at the deadline!


  • Week 1:

    • worksheet_intro and tutorial_intro : Due on Monday, Sep 14


  • Week 2:

    • worksheet_reading and tutorial_reading: Due on Saturday, Sep 19

So far…

  • Introduction to
    • R programming and Jupyter notebooks
    • a sprinkle of data analysis

Now…

Taking our first step in data analysis: loading data into R!

Why use R?

… And why can’t I just use Excel???

vs. 

  • There are many advantages to using R (or another language, like Python or Julia):
    • statistical analysis functions that go beyond Excel
    • free and open-source
    • transparent & reproducible code
    • can handle large amounts of data and complex analyses

Why use R?

  • Using a programming language is like baking with a recipe:
    • Ingredients = data

    • Recipe = code

Data Science Workflow

(source: Grolemund & Wickham, R for Data Science)

Poll 1

Loading/importing data

  • 4 most common ways to do this in Data Science
    1. read in a text file with data in a spreadsheet format
    2. read from a database (e.g., SQLite, PostgreSQL)
    3. scrape data from the web (optional bonus material)
    4. use a web API (Application Programming Interface) to read data from a website (optional bonus material)

File Locations: Local (on your computer)

  • An absolute path locates a file with respect to the “root” folder on a computer
    • starts with /

      e.g. /home/instructor/documents/timesheet.xlsx

  • A relative path locates a file relative to your working directory
    • doesn’t start with /

      e.g. documents/timesheet.xlsx
      (where working directory is /home/instructor/)

File Locations: Remote (on the internet)

via “URL” that starts with http:// or https://

http://traffic.libsyn.com/mbmbam/MyBrotherMyBrotherandMe367.mp3

Absolute vs Relative Paths

  • Generally to ensure your code can be run on a different computer, you should use relative paths

  • Example:

    • Alice is working inside the folder /home/alice/project/.

    • To specify where to load data from in her Jupyter notebook, she uses the absolute path /home/alice/project/data/happiness_report.csv.

    • What issue will arise when she shares the notebook with her collaborator Keeran who tries to read in the data on their computer?

Notes:

Absolute vs Relative Paths

  • What relative data path could they use to collaborate more effectively? Recall Alice loaded the data in using /home/alice/project/data/happiness_report.csv.

Notes:

Poll 2

Demo: Loading Data Locally

Workflow:.

  1. Navigate to the file and see what it looks like

  2. Prepare to load the data into R - Might need to load a library, download a file, or connect to a database

  3. Load the data into R - Using read_csv, read_delim, etc

  4. Inspect the result to make sure it worked!

Let’s load the Old Faithful geyser dataset from Larry Wasserman’s book All of Statistcs

Demo: Loading Data Locally

First, let’s navigate to the file and see what it looks like

Demo: Loading Data Locally

Notes:

Poll 3

Note about loading data

  • It’s important to do it carefully + check results after!
    • will help reduce bugs and speed up your analyses down the road
  • Think of it as tying your shoes before you run; not exciting, but if done wrong it will trip you up later!

Poll 4

Upcoming Due Dates

  • worksheet_reading due THIS SATURDAY 11:59pm (technically early Sunday due to server overloading. Check the due date for your section! )

  • tutorial_reading due THIS SATURDAY 11:59pm (technically early Sunday as well. Check the due date for your section!)

There are NO late submissions/concessions for worksheets. We will drop the lowest worksheet and lowest tutorial score. See policy for late registrants.

Questions?

If not, let’s work together on this week’s worksheet.

  • Want to save these slides? Right click > Print > Save to PDF.
  • Your code and notes will only save if you save as a PDF!