R Read and Write CSV Files

Introduction

CSV (Comma-Separated Values) files are a common format for storing tabular data. They are widely used because they are simple and can be easily imported and exported by various software programs. In this chapter, you will learn how to read from and write to CSV files in R.

Creating a Sample CSV File

Before we start working with CSV files in R, let’s create a sample CSV file. You can create this file using a text editor or any spreadsheet software like Microsoft Excel. Save the file as sample_data.csv with the following content:

Name,Age,Gender,Salary
Alice,30,F,50000
Bob,25,M,45000
Charlie,35,M,55000
Diana,28,F,48000
Eve,40,F,60000

Reading CSV Files

You can read CSV files in R using the read.csv() function. This function reads a file and creates a data frame from it.

Example: Reading a CSV File

Example:

# Reading a CSV file
data <- read.csv("sample_data.csv")
print(data)

Writing to CSV Files

You can write data frames to CSV files in R using the write.csv() function. This function writes a data frame to a file.

Example: Writing to a CSV File

Example:

# Creating a data frame
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "Diana", "Eve"),
  Age = c(30, 25, 35, 28, 40),
  Gender = c("F", "M", "M", "F", "F"),
  Salary = c(50000, 45000, 55000, 48000, 60000)
)

# Writing the data frame to a CSV file
write.csv(data, "output_data.csv", row.names = FALSE)

Customizing CSV Reading and Writing

Specifying Delimiters

By default, read.csv() and write.csv() use commas as delimiters. You can specify a different delimiter using the sep parameter.

Example:

# Reading a CSV file with a different delimiter
data <- read.csv("sample_data_semicolon.csv", sep = ";")
print(data)

# Writing a CSV file with a different delimiter
write.csv(data, "output_data_semicolon.csv", sep = ";", row.names = FALSE)

Handling Missing Values

You can specify how to handle missing values when reading and writing CSV files.

Example:

# Creating a data frame with missing values
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "Diana", "Eve"),
  Age = c(30, NA, 35, 28, 40),
  Gender = c("F", "M", "M", "F", "F"),
  Salary = c(50000, 45000, 55000, NA, 60000)
)

# Writing the data frame to a CSV file with NA replaced by an empty string
write.csv(data, "output_data_missing.csv", na = "", row.names = FALSE)

# Reading the CSV file with NA values
data <- read.csv("output_data_missing.csv", na.strings = "")
print(data)

Example Program Using CSV Files

Here is an example program that demonstrates the reading and writing of CSV files in R.

# R Program to Demonstrate Reading and Writing CSV Files

# Creating a data frame
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "Diana", "Eve"),
  Age = c(30, 25, 35, 28, 40),
  Gender = c("F", "M", "M", "F", "F"),
  Salary = c(50000, 45000, 55000, 48000, 60000)
)

# Writing the data frame to a CSV file
write.csv(data, "output_data.csv", row.names = FALSE)

# Reading the CSV file
data <- read.csv("output_data.csv")
print(data)

# Writing a CSV file with a different delimiter
write.csv(data, "output_data_semicolon.csv", sep = ";", row.names = FALSE)

# Reading a CSV file with a different delimiter
data <- read.csv("output_data_semicolon.csv", sep = ";")
print(data)

# Creating a data frame with missing values
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "Diana", "Eve"),
  Age = c(30, NA, 35, 28, 40),
  Gender = c("F", "M", "M", "F", "F"),
  Salary = c(50000, 45000, 55000, NA, 60000)
)

# Writing the data frame to a CSV file with NA replaced by an empty string
write.csv(data, "output_data_missing.csv", na = "", row.names = FALSE)

# Reading the CSV file with NA values
data <- read.csv("output_data_missing.csv", na.strings = "")
print(data)

Conclusion

In this chapter, you learned how to read from and write to CSV files in R using the read.csv() and write.csv() functions. CSV files are a common format for storing and exchanging tabular data, and being able to work with them is essential for data analysis. By mastering these functions, you can efficiently import and export data in your R programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top