Data Import & Export
R Programming & Data Analytics / Data Import & Export

Data Import & Export

Beginner 10 hrs 4 Concepts
Your Learning Map
📌 You already know
You understand data frames — rows, columns and column types.
🎯 You'll learn here
Reading real files into R (CSV, Excel, databases) and writing your results back out.
🌍 Where it's used
Real analysis never starts from typed-in numbers — you load a government CSV, a hospital export or a bank statement.
M1

Reading Data

Concept 1

Reading CSV with readr

readr's read_csv() is faster than base R's read.csv() and automatically parses data types.

Key arguments: col_types, skip, na, locale, comment.

R
library(readr)
df <- read_csv('students.csv')
df <- read_csv('data.csv', col_types=cols(score=col_double(), name=col_character()), na=c('','NA','N/A'))
R — Read a CSV (no file needed) LIVE READY
Output below is verified. Click to run real R in your browser (first run loads ~20 MB once).
Output (verified)
  name marks
1 Asha    92
2 Ravi    78
3 Sara    85
'data.frame':	3 obs. of  2 variables:
 $ name : chr  "Asha" "Ravi" "Sara"
 $ marks: int  92 78 85
Solved Examples
Example 1 Apply the concept of Reading CSV with readr to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. Which readr function reads a comma-separated file into a tibble?
readr's read_csv() reads a CSV into a tibble; base R's read.csv() is the slower equivalent.
Q2. A key advantage of readr's read_csv() over base read.csv() is that it:
read_csv() is faster and, unlike old read.csv(), never auto-converts character columns to factors.
Concept 2

Importing Excel with readxl

readxl imports .xlsx and .xls files without needing Excel installed.

Use sheet= to pick a sheet and range= to read a specific cell range.

R
library(readxl)
df <- read_excel('report.xlsx', sheet='Q3', range='B2:F50')
excel_sheets('report.xlsx')   # list all sheet names
Solved Examples
Example 1 Apply the concept of Importing Excel with readxl to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. Which package reads .xlsx files without needing Excel or Java installed?
readxl reads .xls/.xlsx files with no dependency on Excel or Java.
Q2. In read_excel(), which argument selects the worksheet to import?
read_excel(path, sheet = ...) chooses the worksheet by name or number.
M2

APIs and Databases

Concept 1

JSON and REST APIs

jsonlite parses JSON. httr makes HTTP requests. Together they handle most REST APIs.

Always check status_code(resp) == 200 before parsing.

R
library(jsonlite); library(httr)
resp <- GET('https://api.example.com/data', add_headers(Authorization='Bearer TOKEN'))
if(status_code(resp)==200) data <- content(resp, as='parsed')
Solved Examples
Example 1 Apply the concept of JSON and REST APIs to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. Which R package parses JSON text into R lists and data frames?
jsonlite::fromJSON() converts JSON into R structures (lists/data frames).
Q2. When calling a REST API in R, which package sends the HTTP request?
httr (e.g. httr::GET()) performs the HTTP request; jsonlite then parses the JSON body.
Concept 2

Writing Data

write_csv (readr) and write.xlsx (openxlsx) export data. Always use write_csv over write.csv for UTF-8 and speed.

R
write_csv(df, 'output.csv')
write_json(df, 'output.json', pretty=TRUE)
# Excel
library(openxlsx)
write.xlsx(df, 'output.xlsx', sheetName='Data')
Solved Examples
Example 1 Apply the concept of Writing Data to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. Which readr function writes a data frame to a CSV file?
readr's write_csv() writes a data frame to CSV (no row names by default).
Q2. To save an R object so it can later be reloaded with its exact structure, use:
saveRDS() serialises any R object to a .rds file; readRDS() restores it exactly.
R Data Structures In Depth Data Wrangling with dplyr