Importing and loading data is a crucial skill in data analysis. R offers various methods to handle different data formats efficiently. This article covers the essential techniques for importing data into R, using datasets from different packages, and explores open data sources and useful resources.
Data Available in Different Packages
R comes with a plethora of packages that include built-in datasets, perfect for learning and practice:
Includes classic datasets like Iris, mtcars, and airquality.
library (datasets)
data (iris)
str (iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Contains datasets such as mpg
for practicing data visualization.
library (ggplot2)
data (mpg)
str (mpg)
tibble [234 × 11] (S3: tbl_df/tbl/data.frame)
$ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
$ model : chr [1:234] "a4" "a4" "a4" "a4" ...
$ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
$ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
$ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
$ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
$ drv : chr [1:234] "f" "f" "f" "f" ...
$ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
$ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
$ fl : chr [1:234] "p" "p" "p" "p" ...
$ class : chr [1:234] "compact" "compact" "compact" "compact" ...
Provides the starwars
and storms
datasets, useful for demonstrating data manipulation techniques.
library (dplyr)
data (starwars)
str (starwars)
tibble [87 × 14] (S3: tbl_df/tbl/data.frame)
$ name : chr [1:87] "Luke Skywalker" "C-3PO" "R2-D2" "Darth Vader" ...
$ height : int [1:87] 172 167 96 202 150 178 165 97 183 182 ...
$ mass : num [1:87] 77 75 32 136 49 120 75 32 84 77 ...
$ hair_color: chr [1:87] "blond" NA NA "none" ...
$ skin_color: chr [1:87] "fair" "gold" "white, blue" "white" ...
$ eye_color : chr [1:87] "blue" "yellow" "red" "yellow" ...
$ birth_year: num [1:87] 19 112 33 41.9 19 52 47 NA 24 57 ...
$ sex : chr [1:87] "male" "none" "none" "male" ...
$ gender : chr [1:87] "masculine" "masculine" "masculine" "masculine" ...
$ homeworld : chr [1:87] "Tatooine" "Tatooine" "Naboo" "Tatooine" ...
$ species : chr [1:87] "Human" "Droid" "Droid" "Human" ...
$ films :List of 87
..$ : chr [1:5] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith" ...
..$ : chr [1:6] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" ...
..$ : chr [1:7] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" ...
..$ : chr [1:4] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith"
..$ : chr [1:5] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith" ...
..$ : chr [1:3] "A New Hope" "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:3] "A New Hope" "Attack of the Clones" "Revenge of the Sith"
..$ : chr "A New Hope"
..$ : chr "A New Hope"
..$ : chr [1:6] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" ...
..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:2] "A New Hope" "Revenge of the Sith"
..$ : chr [1:5] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith" ...
..$ : chr [1:4] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Force Awakens"
..$ : chr "A New Hope"
..$ : chr [1:3] "A New Hope" "Return of the Jedi" "The Phantom Menace"
..$ : chr [1:3] "A New Hope" "The Empire Strikes Back" "Return of the Jedi"
..$ : chr "A New Hope"
..$ : chr [1:5] "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" "Attack of the Clones" ...
..$ : chr [1:5] "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" "Attack of the Clones" ...
..$ : chr [1:3] "The Empire Strikes Back" "Return of the Jedi" "Attack of the Clones"
..$ : chr "The Empire Strikes Back"
..$ : chr "The Empire Strikes Back"
..$ : chr [1:2] "The Empire Strikes Back" "Return of the Jedi"
..$ : chr "The Empire Strikes Back"
..$ : chr [1:2] "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 [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
..$ : chr "The Phantom Menace"
..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
..$ : chr "The Phantom Menace"
..$ : chr "The Phantom Menace"
..$ : chr "The Phantom Menace"
..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
..$ : chr "The Phantom Menace"
..$ : chr "The Phantom Menace"
..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
..$ : chr "The Phantom Menace"
..$ : chr "Return of the Jedi"
..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
..$ : chr "The Phantom Menace"
..$ : chr "The Phantom Menace"
..$ : chr "The Phantom Menace"
..$ : chr "The Phantom Menace"
..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:2] "The Phantom Menace" "Revenge of the Sith"
..$ : chr [1:2] "The Phantom Menace" "Revenge of the Sith"
..$ : chr [1:2] "The Phantom Menace" "Revenge of the Sith"
..$ : chr "The Phantom Menace"
..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
..$ : chr "Attack of the Clones"
..$ : chr "Attack of the Clones"
..$ : chr "Attack of the Clones"
..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
..$ : chr "Attack of the Clones"
..$ : chr "Attack of the Clones"
..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
..$ : chr [1:2] "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 [1:2] "Attack of the Clones" "Revenge of the Sith"
..$ : chr "Attack of the Clones"
..$ : chr "Attack of the Clones"
..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
..$ : chr "Revenge of the Sith"
..$ : chr "Revenge of the Sith"
..$ : chr [1:2] "A New Hope" "Revenge of the Sith"
..$ : chr [1:2] "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"
$ vehicles :List of 87
..$ : chr [1:2] "Snowspeeder" "Imperial Speeder Bike"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Imperial Speeder Bike"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Tribubble bongo"
..$ : chr [1:2] "Zephyr-G swoop bike" "XJ-6 airspeeder"
..$ : chr(0)
..$ : chr "AT-ST"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Snowspeeder"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Tribubble bongo"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Sith speeder"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Flitknot speeder"
..$ : chr(0)
..$ : chr(0)
..$ : chr "Koro-2 Exodrive airspeeder"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Tsmeu-6 personal wheel bike"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
$ starships :List of 87
..$ : chr [1:2] "X-wing" "Imperial shuttle"
..$ : chr(0)
..$ : chr(0)
..$ : chr "TIE Advanced x1"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "X-wing"
..$ : chr [1:5] "Jedi starfighter" "Trade Federation cruiser" "Naboo star skiff" "Jedi Interceptor" ...
..$ : chr [1:3] "Naboo fighter" "Trade Federation cruiser" "Jedi Interceptor"
..$ : chr(0)
..$ : chr [1:2] "Millennium Falcon" "Imperial shuttle"
..$ : chr [1:2] "Millennium Falcon" "Imperial shuttle"
..$ : chr(0)
..$ : chr(0)
..$ : chr "X-wing"
..$ : chr "X-wing"
..$ : chr(0)
..$ : chr(0)
..$ : chr "Slave 1"
..$ : chr(0)
..$ : chr(0)
..$ : chr "Millennium Falcon"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "A-wing"
..$ : chr(0)
..$ : chr "Millennium Falcon"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr [1:3] "Naboo fighter" "H-type Nubian yacht" "Naboo star skiff"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Naboo Royal Starship"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Scimitar"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Jedi starfighter"
..$ : chr(0)
..$ : chr "Naboo fighter"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "Belbullab-22 starfighter"
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr(0)
..$ : chr "X-wing"
..$ : chr(0)
..$ : chr(0)
Offers datasets for applied statistics, including the Boston housing data.
library (MASS)
data (Boston)
str (Boston)
'data.frame': 506 obs. of 14 variables:
$ crim : num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
$ zn : num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
$ indus : num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
$ chas : int 0 0 0 0 0 0 0 0 0 0 ...
$ nox : num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
$ rm : num 6.58 6.42 7.18 7 7.15 ...
$ age : num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
$ dis : num 4.09 4.97 4.97 6.06 6.06 ...
$ rad : int 1 2 2 3 3 3 5 5 5 5 ...
$ tax : num 296 242 242 222 222 222 311 311 311 311 ...
$ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
$ black : num 397 397 393 395 397 ...
$ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
$ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
Contains datasets ideal for regression, ANOVA, and generalized linear models.
library (carData)
data (MplsStops)
str (MplsStops)
'data.frame': 51920 obs. of 14 variables:
$ idNum : Factor w/ 61212 levels "16-395258","16-395296",..: 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 ...
$ date : POSIXct, format: "2017-01-01 00:00:42" "2017-01-01 00:03:07" ...
$ problem : Factor w/ 2 levels "suspicious","traffic": 1 1 2 1 2 2 1 2 2 2 ...
$ MDC : Factor w/ 2 levels "MDC","other": 1 1 1 1 1 1 1 1 1 1 ...
$ citationIssued: Factor w/ 2 levels "NO","YES": NA NA NA NA NA NA NA NA NA NA ...
$ personSearch : Factor w/ 2 levels "NO","YES": 1 1 1 1 1 1 1 1 1 1 ...
$ vehicleSearch : Factor w/ 2 levels "NO","YES": 1 1 1 1 1 1 1 1 1 1 ...
$ preRace : Factor w/ 8 levels "Black","White",..: 3 3 3 3 3 3 3 3 3 3 ...
$ race : Factor w/ 8 levels "Black","White",..: 3 3 2 4 2 4 1 7 2 1 ...
$ gender : Factor w/ 3 levels "Female","Male",..: 3 2 1 2 1 2 2 1 2 2 ...
$ lat : num 45 45 44.9 44.9 45 ...
$ long : num -93.2 -93.3 -93.3 -93.3 -93.3 ...
$ policePrecinct: int 1 1 5 5 1 1 1 2 2 4 ...
$ neighborhood : Factor w/ 87 levels "Armatage","Audubon Park",..: 11 20 84 84 20 20 20 51 59 28 ...
Available Open Data
There are numerous open data sources available for free use:
Useful Online Resources
Here are some essential resources to deepen your understanding:
Dataset in R : Datasets from different package with CSV link and documentation
R for Data Science : A comprehensive guide covering data import techniques.
RDocumentation : Detailed documentation on R packages and functions.
Quick-R : A quick reference for reading and writing data.
Tidyverse Blog : Articles and tutorials on data science using Tidyverse packages.
DataCamp : Online courses on various topics, including data import in R.