Interacting with external data sources is a fundamental skill for any data analyst or scientist. In R, you have a rich ecosystem of functions and packages designed to read and write data in formats ranging from plain text CSVs to Excel workbooks, and even full-fledged SQL databases. This post dives deep into the mechanics, best practices, and advanced techniques for importing and exporting data in R. You’ll learn: How to use read.csv() and write.csv() for tabular data When and why to leverage the readr package for faster I/O Best practices for reading and writing Excel files with readxl , writexl , and openxlsx How to establish database connections via DBI and RSQLite , run queries, and manage transactions Tips for handling large datasets, ensuring reproducibility, and optimizing performance By mastering these tools, you’ll build reproducible pipelines, eliminate manual data wrangling, and streamline collaboration across teams. Let’s get started. Table of Contents Working with CSV ...
R’s true power comes from its rich set of built-in data structures. Choosing the right structure for your task not only streamlines code but also maximizes performance. In this section, we’ll dive deep into: Vectors: the simplest one-dimensional object Factors: efficient categorical data handling Lists: heterogeneous collections for complex data Matrices & Arrays: multi-dimensional atomic vectors Data Frames & Tibbles: tabular data ready for analysis 1. Vectors: The Foundation Vectors are R’s basic building blocks. A vector holds elements of a single type—numeric, character, or logical—and supports vectorized operations for speed and clarity. r # Creating vectors numeric_vec <- c(10, 20, 30, 40) char_vec <- c("red", "green", "blue") logical_vec <- c(TRUE, FALSE, TRUE) # Element-wise operations numeric_vec * 2 # [1] 20 40 60 80 # Indexing and subsetting numeric_vec[2] # 20 numeric_vec[numeric_vec > 25] # 30 40 Best pract...