R Read and Write Text Files

Introduction

Reading from and writing to text files is a fundamental task in data analysis and programming. R provides built-in functions to handle text files efficiently. In this chapter, you will learn how to read from and write to text files using the base R functions read.table(), readLines(), write.table(), and writeLines().

Reading Text Files

You can read text files in R using the read.table() and readLines() functions.

Using read.table()

The read.table() function reads a file and creates a data frame from it. This function is particularly useful for reading tabular data.

Example:

# Reading a text file with read.table()
data <- read.table("sample_data.txt", header = TRUE, sep = "\t")
print(data)

Using readLines()

The readLines() function reads a file line by line and returns a character vector where each element is a line from the file.

Example:

# Reading a text file with readLines()
lines <- readLines("sample_data.txt")
print(lines)

Writing Text Files

You can write data frames to text files in R using the write.table() and writeLines() functions.

Using write.table()

The write.table() function writes a data frame to a file. This function is useful for writing tabular data.

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 text file
write.table(data, "output_data.txt", sep = "\t", row.names = FALSE)

Using writeLines()

The writeLines() function writes a character vector to a file, where each element of the vector is written as a separate line in the file.

Example:

# Creating a character vector
lines <- c("Line 1", "Line 2", "Line 3", "Line 4", "Line 5")

# Writing the character vector to a text file
writeLines(lines, "output_lines.txt")

Example Program Using Text Files

Here is an example program that demonstrates the reading and writing of text files in R using the base R functions read.table(), readLines(), write.table(), and writeLines().

Sample Data

Create a sample text file named sample_data.txt 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

Example Program

# R Program to Demonstrate Reading and Writing Text Files

# Reading a text file with read.table()
data <- read.table("sample_data.txt", header = TRUE, sep = "\t")
print("Data read using read.table():")
print(data)

# Reading a text file with readLines()
lines <- readLines("sample_data.txt")
print("Data read using readLines():")
print(lines)

# 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 text file with write.table()
write.table(data, "output_data.txt", sep = "\t", row.names = FALSE)
print("Data written to output_data.txt using write.table()")

# Creating a character vector
lines <- c("Line 1", "Line 2", "Line 3", "Line 4", "Line 5")

# Writing the character vector to a text file with writeLines()
writeLines(lines, "output_lines.txt")
print("Lines written to output_lines.txt using writeLines()")

Conclusion

In this chapter, you learned how to read from and write to text files in R using the read.table(), readLines(), write.table(), and writeLines() functions. These functions are essential for handling text data in R and can be used for various tasks such as reading tabular data, processing line-by-line text, and writing results to text files. By mastering these functions, you can efficiently manage text data in your R programs.

Leave a Comment

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

Scroll to Top