Examples of Reading and Writing CSV Files

Explore practical R code snippets for reading and writing CSV files, enhancing your data manipulation skills.
By Jamie

Introduction to Reading and Writing CSV Files in R

CSV (Comma-Separated Values) files are a common format for storing tabular data. In R, reading and writing these files is straightforward, making it an essential skill for data manipulation and analysis. Below are three practical examples demonstrating how to read from and write to CSV files using R, ensuring you can handle your data efficiently.

Example 1: Reading a CSV File into R

Context

In this example, we will read a CSV file containing a dataset of student scores. This is useful for data analysis tasks such as calculating averages or identifying trends.

# Load necessary library
library(readr)

# Read the CSV file
student_scores <- read_csv("student_scores.csv")

# Display the first few rows of the dataset
head(student_scores)

In this code snippet, we use the read_csv function from the readr package to read the CSV file named student_scores.csv. The head function displays the first six rows of the dataset, allowing us to quickly inspect the data.

Notes

  • Ensure the readr package is installed using install.packages("readr").
  • The function automatically infers the column types, making it user-friendly.

Example 2: Writing a Data Frame to a CSV File

Context

In this example, we will create a data frame containing sales data and write it to a new CSV file. This is useful when you want to export your analysis results for reporting or sharing.

# Create a data frame
sales_data <- data.frame(
  Product = c("A", "B", "C"),
  Sales = c(150, 200, 300),
  Quarter = c("Q1", "Q1", "Q1")
)

# Write the data frame to a CSV file
write_csv(sales_data, "sales_data.csv")

In this snippet, we create a simple data frame named sales_data with product sales information. We then use the write_csv function to write this data frame into a new CSV file called sales_data.csv.

Notes

  • You can customize the output by specifying additional parameters in write_csv, such as setting na to represent missing values.
  • Using write.csv instead of write_csv can also be an option, but it may require additional parameters for better formatting.

Example 3: Appending Data to an Existing CSV File

Context

This example demonstrates how to append new data to an existing CSV file. This is particularly useful for logging or updating datasets without overwriting existing information.

# Load necessary library
library(readr)

# New data to append
new_sales_data <- data.frame(
  Product = c("D", "E"),
  Sales = c(400, 250),
  Quarter = c("Q2", "Q2")
)

# Append the new data to the existing CSV file
write_csv(new_sales_data, "sales_data.csv", append = TRUE)

In this code, we define a new data frame new_sales_data with additional sales information. By using the write_csv function with the append = TRUE argument, we add this new data to the existing sales_data.csv without losing the original data.

Notes

  • Ensure that the columns in the new data frame match those in the existing CSV file.
  • This method can be practical for ongoing data collection, such as logging daily sales.