Importing and Exporting data in R

Data Science Tutorials
R Programming
Author

TheRimalaya

Published

March 1, 2017

Modified

October 7, 2024

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.

Different Formats of Data

Data comes in various formats, each suited for different scenarios:

  • CSV (Comma Separated Values): Simple text files where values are separated by commas.
  • Excel: Commonly used .xls and .xlsx files.
  • SQL Databases: Structured data stored in tables within relational databases.
  • JSON (JavaScript Object Notation): Lightweight data interchange format.
  • HTML: Webpage data.
  • SPSS, SAS, Stata: Formats used by specialized statistical software.
  • RData and RDS: Native R formats for storing R objects.

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 ...

Importing Different Data Formats into R

Before diving into examples, let’s introduce some useful packages for data import:

  • haven: For SPSS, SAS, and Stata files.
  • data.table: Efficient data manipulation and import.
  • readxl: For reading Excel files.
  • jsonlite: For importing JSON files.

Here is how you can import different data formats into R:

data <- read.csv("data/airtravel.csv")
head(data)
  Month X1958 X1959 X1960
1   JAN   340   360   417
2   FEB   318   342   391
3   MAR   362   406   419
4   APR   348   396   461
5   MAY   363   420   472
6   JUN   435   472   535
library(readxl)
data <- read_excel("data/Melanoma.xlsx")
head(data)
# A tibble: 6 × 7
   time status   sex   age  year thickness ulcer
  <dbl>  <dbl> <dbl> <dbl> <dbl>     <dbl> <dbl>
1    10      3     1    76  1972      6.76     1
2    30      3     1    56  1968      0.65     0
3    35      2     1    41  1977      1.34     0
4    99      3     0    71  1968      2.9      0
5   185      1     1    52  1965     12.1      1
6   204      1     1    28  1971      4.84     1
library(DBI)
con <- dbConnect(RSQLite::SQLite(), "data/medal.db")
data <- dbGetQuery(con, "SELECT * FROM Olympic2024")
head(data)
               Team              Olympic No Gold Silver Bronze Total
1 Afghanistan (AFG)       Combined total 16    0      0      2     2
2 Afghanistan (AFG) Summer Olympic Games 16    0      0      2     2
3 Afghanistan (AFG) Winter Olympic Games  0    0      0      0     0
4     Albania (ALB)       Combined total 15    0      0      2     2
5     Albania (ALB) Summer Olympic Games 10    0      0      2     2
6     Albania (ALB) Winter Olympic Games  5    0      0      0     0
library(jsonlite)
data <- fromJSON("data/sdg-goals.json")
str(data, list.len = 2)
'data.frame':   17 obs. of  6 variables:
 $ goal     :List of 17
  ..$ : int 1
  ..$ : int 2
  .. [list output truncated]
 $ title    :List of 17
  ..$ : chr "End poverty in all its forms everywhere"
  ..$ : chr "End hunger, achieve food security and improved nutrition and promote sustainable agriculture"
  .. [list output truncated]
  [list output truncated]
library(rvest)
# url <- "https://en.wikipedia.org/wiki/Booker_Prize"
url <- "data/Booker-Prize.html"
data <- read_html(url) %>%
    html_node(".wikitable") %>%
    html_table()
head(data)
# A tibble: 6 × 5
   Year Author              Title                   `Genre(s)`       hideCountry
  <int> <chr>               <chr>                   <chr>            <chr>      
1  1969 P. H. Newby[62]     Something to Answer For Literary fiction UK         
2  1970 Bernice Rubens[63]  The Elected Member      Literary fiction UK         
3  1971 V. S. Naipaul[64]   In a Free State         Literary fiction UK TTO     
4  1972 John Berger[65]     G.                      Experimental li… UK         
5  1973 J. G. Farrell[66]   The Siege of Krishnapur Literary fiction UK IRL     
6  1974 Nadine Gordimer[67] The Conservationist     Literary fiction ZAF        

We can load RData file with load function and Rds file using readRDS function. RData file can contain multiple R objects and when loaded we can find the objects saved in RData file in R environment.

load("data/cancer.rds")
head(cancer)
  inst time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss
1    3  306      2  74   1       1       90       100     1175      NA
2    3  455      2  68   1       0       90        90     1225      15
3    3 1010      1  56   1       0       90        90       NA      15
4    5  210      2  57   1       1       90        60     1150      11
5    1  883      2  60   1       0      100        90       NA       0
6   12 1022      1  74   1       1       50        80      513       0
popmort <- readRDS("data/popmort.rds")
str(popmort)
 'ratetable' num [1:110, 1:2, 1:81] 1.47e-04 1.52e-05 7.92e-06 5.51e-06 4.44e-06 ...
 - attr(*, "dimnames")=List of 3
  ..$ age : chr [1:110] "0" "1" "2" "3" ...
  ..$ sex : chr [1:2] "male" "female"
  ..$ year: chr [1:81] "1940" "1941" "1942" "1943" ...
 - attr(*, "type")= num [1:3] 2 1 4
 - attr(*, "cutpoints")=List of 3
  ..$ : num [1:110] 0 365 730 1096 1461 ...
  ..$ : NULL
  ..$ : Date[1:81], format: "1940-01-01" "1941-01-01" ...
 - attr(*, "summary")=function (R)  
  ..- attr(*, "srcref")= 'srcref' int [1:8] 7 13 15 3 13 3 7 15
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5d4b1a8a8440> 
library(haven)
spss_file <- read_sav("data/melanoma.sav")
head(spss_file)
# A tibble: 6 × 14
  sex          age stage     mmdx  yydx surv_mm surv_yy status  subsite year8594
  <dbl+lbl>  <dbl> <dbl+lb> <dbl> <dbl>   <dbl>   <dbl> <dbl+l> <dbl+l> <dbl+lb>
1 2 [Female]    81 1 [Loca…     2  1981    26.5     2.5 2 [Dea… 1 [Hea… 0 [Diag…
2 2 [Female]    75 1 [Loca…     9  1975    55.5     4.5 2 [Dea… 1 [Hea… 0 [Diag…
3 2 [Female]    78 1 [Loca…     2  1978   178.     14.5 2 [Dea… 3 [Lim… 0 [Diag…
4 2 [Female]    75 0 [Unkn…     8  1975    29.5     2.5 1 [Dea… 4 [Mul… 0 [Diag…
5 2 [Female]    81 0 [Unkn…     7  1981    57.5     4.5 2 [Dea… 1 [Hea… 0 [Diag…
6 2 [Female]    75 1 [Loca…     9  1975    19.5     1.5 1 [Dea… 2 [Tru… 0 [Diag…
# ℹ 4 more variables: dx <date>, exit <date>, agegrp <dbl+lbl>, id <dbl>
stata_file <- read_dta("data/colon.dta")
head(stata_file)
# A tibble: 6 × 14
  sex          age stage     mmdx  yydx surv_mm surv_yy status  subsite year8594
  <dbl+lbl>  <dbl> <dbl+lb> <dbl> <dbl>   <dbl>   <dbl> <dbl+l> <dbl+l> <dbl+lb>
1 2 [Female]    77 3 [Dist…     3  1977    16.5     1.5 1 [Dea… 2 [Tra… 0 [Diag…
2 2 [Female]    78 1 [Loca…     7  1978    82.5     6.5 2 [Dea… 1 [Coe… 0 [Diag…
3 1 [Male]      78 3 [Dist…    10  1978     1.5     0.5 1 [Dea… 3 [Des… 0 [Diag…
4 1 [Male]      76 3 [Dist…    10  1976     1.5     0.5 1 [Dea… 3 [Des… 0 [Diag…
5 1 [Male]      80 1 [Loca…    12  1980     8.5     0.5 1 [Dea… 3 [Des… 0 [Diag…
6 2 [Female]    75 1 [Loca…    11  1975    23.5     1.5 1 [Dea… 1 [Coe… 0 [Diag…
# ℹ 4 more variables: agegrp <dbl+lbl>, dx <date>, exit <date>, id <dbl>
sas_file <- read_xpt("data/mpg.xpt")
head(sas_file)
# A tibble: 6 × 11
  manufacturer model displ  year   cyl trans      drv     cty   hwy fl    class 
  <chr>        <chr> <dbl> <dbl> <dbl> <chr>      <chr> <dbl> <dbl> <chr> <chr> 
1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p     compa…
2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p     compa…
3 audi         a4      2    2008     4 manual(m6) f        20    31 p     compa…
4 audi         a4      2    2008     4 auto(av)   f        21    30 p     compa…
5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p     compa…
6 audi         a4      2.8  1999     6 manual(m5) f        18    26 p     compa…

Exporting Data to Different Formats in R

R also allows you to export data to various formats:

write.csv(data, "data.csv")
library(writexl)
write_xlsx(data, "data.xlsx")
library(DBI)
con <- dbConnect(RSQLite::SQLite(), "example.db")
dbWriteTable(con, "tablename", data)
library(jsonlite)
write_json(data, "data.json")
library(xml2)
write_html(as_xml_document(as.character(data)), "data.html")
save(data, file = "data.RData")
saveRDS(data, "data.rds")
library(haven)
write_sav(data, "data.sav") # SPSS file
library(haven)
write_dta(data, "data.dta") # Stata file
library(haven)
write_xpt(data, "data.xpt") # SAS file

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.