R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
172: Reading and Writing Parquet Files in R
I've seen this happen in almost every production environment I've touched: a developer starts a project using CSVs because they're easy to peek at in a text editor. Everything works fine when the dataset is 10,000 rows. But then the project scales to 10 million rows, and suddenly, the "simple" data loading step takes five minutes, the server runs out of RAM, and you spend half your morning fixing column types that shifted from integers to doubles during the save-and-load cycle.
The CSV Trap: Why your disk is screaming
If you're still using write.csv() and read.csv() for large datasets, you're fighting a losing battle. Let's say we're working with a massive dataset of global weather sensor readings—millions of rows with timestamps, sensor IDs, and temperature readings. In the naive approach, you'd do something like this:
# The slow, memory-heavy way
write.csv(weather_data, "weather_readings.csv", row.names = FALSE)
# Later...
weather_data <- read.csv("weather_readings.csv")
Here is the problem: CSVs are plain text. Every single number is stored as a string of characters. To R, the number 23.456789 isn't a double; it's eight separate characters. This bloats your file size and forces R to "guess" the data types every time you read the file back in. I can't tell you how many bugs I've chased down because a column of IDs was read as numeric instead of character, causing leading zeros to vanish into thin air.
Switching to Parquet for Actual Performance
This is where Parquet comes in. Unlike CSVs, Parquet is a columnar storage format. Instead of storing data row-by-row, it stores it column-by-column and compresses it heavily. For our weather data, this is a game-changer. If you only need the temperature column, Parquet allows the system to skip over the sensor_id and timestamp columns entirely on the disk.
To do this in R, we use the arrow package. It's the industry standard for handling this format. Here is how I would handle that same weather dataset:
library(arrow)
# Writing the data - this is significantly faster and the file is tiny
write_parquet(weather_data, "weather_readings.parquet")
# Reading the data - types are preserved exactly as they were
weather_data <- read_parquet("weather_readings.parquet")
When you run this, you'll notice two things immediately. First, the file size on your hard drive will plummet—often by 70% to 90% compared to a CSV. Second, the loading time is almost instantaneous. More importantly, the schema is baked into the file. Your dates stay dates, your integers stay integers, and you don't have to pass colClasses to read.csv just to keep your data from breaking.
The Trade-off: Readability vs. Efficiency
Now, I'm not telling you to delete every CSV you own. Parquet is a binary format; you can't open it in Notepad or TextEdit to see if "the data looks right." It's an opaque blob to a human.
If you're exporting a small summary table for a non-technical stakeholder to open in Excel, stick with CSV. It's the universal language of spreadsheets. But for any internal data pipeline, intermediate storage between scripts, or datasets that exceed a few hundred megabytes, Parquet is the only professional choice. I generally follow a simple rule: if the file is for a human, CSV; if the file is for a machine, Parquet.
📋 Practical Task
Exercise: Optimizing the Sensor Archive
You have been handed a legacy dataset containing 1 million simulated sensor readings. Your goal is to prove the efficiency of Parquet over CSV by comparing their footprints and loading speeds.
Requirements:
- Create a dataframe called
sensor_logswith 1,000,000 rows. It should have three columns:timestamp(POSIXct),sensor_id(a factor or character), andvalue(numeric). - Write this dataframe to a file named
logs.csvand then to a file namedlogs.parquetusing thearrowpackage. - Use
file.info()$sizeto compare the file sizes of the two resulting files and print the difference. - Use
system.time()to measure how long it takes to read the CSV back into R versus reading the Parquet file. - Verify that the
timestampcolumn in the Parquet-loaded dataframe is still aPOSIXctobject, whereas the CSV-loaded version requires conversion.
There are no comments for now.