Import CSV

  • Will run this code together
  • ~15 Minutes
  • Followed By: Another Topic!
The Titanic

Learning Objectives

  • Import CSV data
    Rest of today's workshop uses a sample of the passenger manifest (CSV) from the Titanic
  • How to tell if we imported the data successfully
    • Hint #1: We should have data on 250 passengers.
    • Hint #2: We should have 13 columns of data.
  • Discuss: R Sessions

All Titanic data sampled from
Kaggle's Machine Learning demo

Import Passengers Data

This could be one line of code,
but it would run off my slide.


url <- "http://choens.github.io/titanic/public/data/passengers.csv"
passengers <- read.csv(file = url)
    

Photo of Captain Smith Your Turn!

Did it work?

> dim(passengers)
                

> str(passengers)
                

Hint: Answers on next slide

Photo of Captain Smith Your Turn!

Tell us NOW if you can't get this to work!

> dim(passengers)
[1] 250  13
                

> str(passengers)
'data.frame':	250 obs. of  13 variables:
 $ passenger_id      : int  104 551 540 890 762 567 9 205 587 442 ...
 $ survived          : int  0 1 1 1 0 0 1 1 0 0 ...
 $ passenger_class   : int  3 1 1 1 3 3 3 3 2 3 ...
 $ passenger_name    : Factor w/ 250 levels "Abbing, Mr. Anthony",..: 118 217 70 26 168 209 121 46 113 88 ...
 $ sex               : Factor w/ 2 levels "female","male": 2 2 1 2 2 2 1 2 2 2 ...
 $ age               : num  33 17 22 26 41 19 27 18 47 20 ...
 $ child             : int  0 1 0 0 0 0 0 0 0 0 ...
                

Congratulations!

You can import CSV data

Discuss: R Sessions

  • You (should) have a data.frame called passengers in the current R session
  • We will spend a couple of hours manipulating it
  • Don't delete it or restart your R session
  • If you do, you have to re-run import-passengers.R
  • We will run code from other R scripts, and that is fine, as long as we don't change the R session
RStudio icon