How to Read a Text File Line by Line in R

Jesse John Feb 02, 2024
How to Read a Text File Line by Line in R

In R programming, reading text files line by line is a common operation in data analysis and manipulation tasks. This article will discuss the common method used to read a text file line by line in R.

Use the readLines() Function to Read a Text File Line by Line in R

We will use the readLines() function as long as lines are read. Because we do not know the number of lines, we need to know when the end of the file is reached.

The readLines() function returns an empty character vector character(0) when no more lines are in the file.

At this point, we will exit the loop with a break. Note that the loop will not break on blank lines, as these have the end-of-line markers.

We will use the identical() function on each loop iteration to check whether the line is identical to a character(0).

If it is, we break; otherwise, we process the line. In the example, we print it out.

According to the documentation, the last line of the text file should have an end-of-line marker for the readLines() function to work consistently.

First, create a sample text file with the following text and save it with UTF-8 encoding and a suitable file name (we need to replace the file name in the code accordingly).

The first line.
The second line.
The third line.

Last line.

In the example code, we will do the following.

  • Create a connection and open it.
  • Loop over the file, one line at a time, printing it.
  • Exit the loop when the line is a character(0).
  • Close the connection. An explicitly opened connection needs to be explicitly closed.

Example Code:

# Explicitly create and open a connection.
# REPLACE THE FILE NAME AS PER YOUR FILE.
myCon = file(description = "filename.txt", open="r", blocking = TRUE)

# The position in the connection advances to the next line on each iteration.
# Loop till the line is the empty vector, character(0).
repeat{
  pl = readLines(myCon, n = 1) # Read one line from the connection.
  if(identical(pl, character(0))){break} # If the line is empty, exit.
  print(pl) # Otherwise, print and repeat next iteration.
}

# Explicitly opened connection needs to be explicitly closed.
close(myCon)
rm(myCon) # Removes the connection object from memory.

Output:

The first line.
The second line.
The third line.

Last line.

Let’s have another example.

Example Code:

# Specify the path to your text file
file_path <- "your_file.txt"

# Use the readLines function to read the file line by line
# It reads the entire content of the specified file and stores each line as an element in a character vector named lines
lines <- readLines(file_path)

# Loop through the lines and process them
for (line in lines) {
  # Process each line here (e.g., print it)
  cat(line, "\n")
}

When you run this code, it will read the text file line by line and display each line.

Output:

The first line.
The second line.
The third line.

Last line.

Remember to replace "your_file.txt" with the actual file path of the sample text file you intend to work with.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

Related Article - R File