There are many ways to store data. Here are some of the most common ways to read in data using the readr package.

# Load the readr package
library(readr)

read_delim

The read_delim() function will read in data from many different file types. The code below reads in data from a file saved on the web. The web address is saved in a variable called web.url.

  • The col_names = TRUE indicates that the first row of the file contains the variable names.

  • The delim="," argument indicates that the file contains variable names and data values that are separated, delimited, by commas.

  • Thetrim_ws=TRUE argument removes all spaces from the variable name.

# data url
web.url = "http://homepages.gac.edu/~anienow2/MCS_142/R/example.csv"

# reads in the data and saves it to a variable 
the.data <- read_delim(file = web.url, col_names = TRUE, delim = ",", trim_ws= TRUE) 
## 
## ── Column specification ───────────────────────────────────────────────────────────────────────────
## cols(
##   Name = col_character(),
##   Height = col_double(),
##   Eye.Color = col_character(),
##   Hand = col_character(),
##   Gender = col_character()
## )

Look at the output above. The read_delim() function guesses at the variable type to use for each column. In this case it guessed correctly that Height was a quantitative variable, but guessed incorrectly about Eye.Color, Hand, and Gender. See the Modifying Data tutorial to fix this.

# Look at the data
the.data 
## # A tibble: 5 x 5
##   Name   Height Eye.Color Hand  Gender
##   <chr>   <dbl> <chr>     <chr> <chr> 
## 1 Shelly     55 Blue      Right F     
## 2 Alvin      60 Green     Left  M     
## 3 Simon      67 Blue      Right M     
## 4 Theo       72 Green     Right M     
## 5 Nicki      54 Brown     Right F

read_csv

The read_csv() function is a special case of the read_delim() function. The .csv extension is short for comma separated values. This function assumes the file is delimited with commas. The code below uses the read_csv() function instead of the read_delim() function to read in the file stored at web.url.

# data url
web.url = "http://homepages.gac.edu/~anienow2/MCS_142/R/example.csv"

# reads in the data and saves it to a variable 
the.data <- read_csv(file = web.url, col_names = TRUE, trim_ws= TRUE) 
## 
## ── Column specification ───────────────────────────────────────────────────────────────────────────
## cols(
##   Name = col_character(),
##   Height = col_double(),
##   Eye.Color = col_character(),
##   Hand = col_character(),
##   Gender = col_character()
## )

read_excel

The read_excel() function in the readxl package will read in data from an excel spreadsheet (.xlsx). The data from this function is stored as a tibble. See the help page ?readxl::read_excel for a more detailed listing of arguments and usage for this function and package. Below is example code for a simple use case.

# Loads the library
library(readxl)  

# reads in the data from a file
the.data <- read_excel(path="./example.xlsx", sheet=1)

Loading Local Data

The code below will allow you to read data from a file that is stored locally on your computer. The ./ in front of the file name indicates that the file is located in the current directory. You may need to move the file or change the working directory to get this to work.

# reads in the data from a file
the.data <- read_csv(file="./example.csv", col_names = TRUE, trim_ws=TRUE)

Mathematicss, Computer Science, and Statistics Department Gustavus Adolphus College