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

Data Import & Export

Beginner 10 hrs 4 Concepts
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'))
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. What is the primary purpose of reading csv with readr?
Q2. Which R package is most relevant for this topic?
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. What is the primary purpose of importing excel with readxl?
Q2. Which R package is most relevant for this topic?
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. What is the primary purpose of json and rest apis?
Q2. Which R package is most relevant for this topic?
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. What is the primary purpose of writing data?
Q2. Which R package is most relevant for this topic?
R Data Structures In Depth Data Wrangling with dplyr