Let’s focus on the Tidyverse. Tidyverse is a set of packages that work together within a common tidy-data framework.1 These packages modernize R and enable an efficient workflow. Although this method is not the only way to use R, you may work for a long time before you need to explore beyond the tidyverse. This tidy framework is great for learning R and being productive when using R. Let’s get started.

 

Load library Packages:

library(tidyverse)

 

Note. You must install a library-package before you load a library-package. The above code chunk engages the library() function to load the “tidyverse” packages.

As you see from the image above, the tidyverse package is a super-package consisting of several more precise and focused packages. You can install packages from the Tools menu-bar in the RStudio IDE.

Load Custom Data

Outside of this workshop, you’ll likely want to load your own data. R and RStudio support many methods of gathering and importing data. Two common data import methods include importing data from the local file system or via a URL.

RStudio has an on-board data loading wizard which leverages the readr() package for data loading. There are several ways to engage this import function:

  • File > Import Dataset
  • Or, via the Import Dataset “button” found in the Environment Pane.
RStudio Environment Pane
RStudio Environment Pane


Using the Import wizard, you can generate (and execute) the code necessary to read in the cars.csv file.

# readr::read_csv
# 'read_csv()' is part of the tidyverse 'readr' package.  

cars <- read_csv("data/cars.csv")

‘reader::read_csv()’ helpfully overrides default behavior of the “base R” read.csv() function. For example read_csv() does not automatically convert strings as factors. You can read more about readr http://readr.tidyverse.org/

Workshop Data Set

During this workshop demonstration, We’ll use the Starwars characters dataset, starwars. The data, part of the dplyr package, come from SWAPI, the Star Wars API, http://swapi.co/. Since the data are integrated into dplyr, we don’t need to load the data, but you may still want to find information about the dataset. The codebood for the Starwars dataset is integrated into the dplyr documentation. To view the Starwars codebook, first load the library(dplyr) package. Then, via the Help pane, search starwars. Alternatively, in the Console pane, type ?starwars.

Note: dplyr is part of tidyverse which we loaded already.

In the next module, Data Wrangling, we’ll discus the dplyr package in greater detail.

View your data in a grid.

Other Data Loaders, a selective list

  • View() is an exploratory convenience; it’s not “reproducible” in the sense of generating a report or leaving a trail of your work. One of the advantages is on-board clickable sorting

  • Environment Pane has information about data objects

Modern Data Frames: Tibbles

A tibble (tbl) is a “Table as data frame”, a modern tidyverse table.

class(starwars) 
## [1] "tbl_df"     "tbl"        "data.frame"
starwars

 

Data Structure

Most Common Data Structures

  • Vector
  • Data Frame
  • Tibble
  • List
  • Matrix

Class

class() identifies the class of an object revealing the data structure or the data type.

class(starwars)
## [1] "tbl_df"     "tbl"        "data.frame"

Glimpse into a data frame

glimpse() reveals the structure of an object

glimpse(starwars)
## Observations: 87
## Variables: 13
## $ name       <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", ...
## $ height     <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188...
## $ mass       <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 8...
## $ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "b...
## $ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "l...
## $ eye_color  <chr> "blue", "yellow", "red", "yellow", "brown", "blue",...
## $ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0...
## $ gender     <chr> "male", NA, NA, "male", "female", "male", "female",...
## $ homeworld  <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alder...
## $ species    <chr> "Human", "Droid", "Droid", "Human", "Human", "Human...
## $ films      <list> [<"Revenge of the Sith", "Return of the Jedi", "Th...
## $ vehicles   <list> [<"Snowspeeder", "Imperial Speeder Bike">, <>, <>,...
## $ starships  <list> [<"X-wing", "Imperial shuttle">, <>, <>, "TIE Adva...

 

Data Type

Most Common Data Types

  • character
  • numeric

    • integer (e.g. 30L, as.integer(30))
    • double (approximation with floating points and multiple special values)
  • logical (True or False)
  • factor

Class

class(starwars$name)
## [1] "character"
class(starwars$height)
## [1] "integer"
class(starwars$mass)
## [1] "numeric"

Other Useful Functions

tbl_vars lists table variables (column headers) as a vector

tbl_vars(starwars)
##  [1] "name"       "height"     "mass"       "hair_color" "skin_color"
##  [6] "eye_color"  "birth_year" "gender"     "homeworld"  "species"   
## [11] "films"      "vehicles"   "starships"

 

Older style

str() identifies the data structure: data frames, matrices, lists, etc.

str(starwars)
## Classes 'tbl_df', 'tbl' and 'data.frame':    87 obs. of  13 variables:
##  $ name      : chr  "Luke Skywalker" "C-3PO" "R2-D2" "Darth Vader" ...
##  $ height    : int  172 167 96 202 150 178 165 97 183 182 ...
##  $ mass      : num  77 75 32 136 49 120 75 32 84 77 ...
##  $ hair_color: chr  "blond" NA NA "none" ...
##  $ skin_color: chr  "fair" "gold" "white, blue" "white" ...
##  $ eye_color : chr  "blue" "yellow" "red" "yellow" ...
##  $ birth_year: num  19 112 33 41.9 19 52 47 NA 24 57 ...
##  $ gender    : chr  "male" NA NA "male" ...
##  $ homeworld : chr  "Tatooine" "Tatooine" "Naboo" "Tatooine" ...
##  $ species   : chr  "Human" "Droid" "Droid" "Human" ...
##  $ films     :List of 87
##   ..$ : chr  "Revenge of the Sith" "Return of the Jedi" "The Empire Strikes Back" "A New Hope" ...
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith" "Return of the Jedi" ...
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith" "Return of the Jedi" ...
##   ..$ : chr  "Revenge of the Sith" "Return of the Jedi" "The Empire Strikes Back" "A New Hope"
##   ..$ : chr  "Revenge of the Sith" "Return of the Jedi" "The Empire Strikes Back" "A New Hope" ...
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith" "A New Hope"
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith" "A New Hope"
##   ..$ : chr "A New Hope"
##   ..$ : chr "A New Hope"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith" "Return of the Jedi" ...
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr  "Revenge of the Sith" "A New Hope"
##   ..$ : chr  "Revenge of the Sith" "Return of the Jedi" "The Empire Strikes Back" "A New Hope" ...
##   ..$ : chr  "Return of the Jedi" "The Empire Strikes Back" "A New Hope" "The Force Awakens"
##   ..$ : chr "A New Hope"
##   ..$ : chr  "The Phantom Menace" "Return of the Jedi" "A New Hope"
##   ..$ : chr  "Return of the Jedi" "The Empire Strikes Back" "A New Hope"
##   ..$ : chr "A New Hope"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith" "Return of the Jedi" ...
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith" "Return of the Jedi" ...
##   ..$ : chr  "Attack of the Clones" "Return of the Jedi" "The Empire Strikes Back"
##   ..$ : chr "The Empire Strikes Back"
##   ..$ : chr "The Empire Strikes Back"
##   ..$ : chr  "Return of the Jedi" "The Empire Strikes Back"
##   ..$ : chr "The Empire Strikes Back"
##   ..$ : chr  "Return of the Jedi" "The Force Awakens"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr  "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr  "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr  "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Revenge of the Sith"
##   ..$ : chr "Revenge of the Sith"
##   ..$ : chr  "Revenge of the Sith" "A New Hope"
##   ..$ : chr  "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Revenge of the Sith"
##   ..$ : chr "The Force Awakens"
##   ..$ : chr "The Force Awakens"
##   ..$ : chr "The Force Awakens"
##   ..$ : chr "The Force Awakens"
##   ..$ : chr "The Force Awakens"
##   ..$ : chr  "Attack of the Clones" "The Phantom Menace" "Revenge of the Sith"
##  $ vehicles  :List of 87
##   ..$ : chr  "Snowspeeder" "Imperial Speeder Bike"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Imperial Speeder Bike"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Tribubble bongo"
##   ..$ : chr  "Zephyr-G swoop bike" "XJ-6 airspeeder"
##   ..$ : chr 
##   ..$ : chr "AT-ST"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Snowspeeder"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Tribubble bongo"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Sith speeder"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Flitknot speeder"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Koro-2 Exodrive airspeeder"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Tsmeu-6 personal wheel bike"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##  $ starships :List of 87
##   ..$ : chr  "X-wing" "Imperial shuttle"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "TIE Advanced x1"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "X-wing"
##   ..$ : chr  "Jedi starfighter" "Trade Federation cruiser" "Naboo star skiff" "Jedi Interceptor" ...
##   ..$ : chr  "Trade Federation cruiser" "Jedi Interceptor" "Naboo fighter"
##   ..$ : chr 
##   ..$ : chr  "Millennium Falcon" "Imperial shuttle"
##   ..$ : chr  "Millennium Falcon" "Imperial shuttle"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "X-wing"
##   ..$ : chr "X-wing"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Slave 1"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Millennium Falcon"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "A-wing"
##   ..$ : chr 
##   ..$ : chr "Millennium Falcon"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Naboo Royal Starship"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Scimitar"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Jedi starfighter"
##   ..$ : chr 
##   ..$ : chr "Naboo fighter"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "Belbullab-22 starfighter"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr "T-70 X-wing fighter"
##   ..$ : chr 
##   ..$ : chr 
##   ..$ : chr  "H-type Nubian yacht" "Naboo star skiff" "Naboo fighter"
 
R We Having Fun Yet‽ -- Learning Series
Data & Visualization Services
Duke University Libraries
C bn
Shareable via Creative Commons: CC By-NC