Introduction and Context Setting

Welcome to this lesson, where we'll explore an essential technique in text data manipulation: reading files line-by-line. In many real-world applications, processing data one line at a time is crucial for effective data management, particularly when dealing with large files like logs or data streams. By the end of this lesson, you'll understand how to efficiently read and process file data line-by-line, leveraging R's powerful functionality.

Recall: Reading Files

As a quick reminder from our previous lesson, let's revisit how reading files in R works. File handling is done using the readLines() function. You specify the file path, and it returns the content of the file as a character vector. Let's see how it works:

In this example, file_path is a string that indicates the location of your file, and lines is a character vector created by readLines(). This sets up the stage for reading files line-by-line without directly opening or closing the file since the readLines() function handles file access efficiently. Note that readLines() does not include newline characters in the output vector.

For example, consider these file contents:

After executing readLines() on it, we will get the following vector: c("Hello,", " world", "!"), without having to manually handle newline characters.

Iterating Over Lines and Cleaning Up Output

Once you have your file lines in a vector, you can iterate over them using a for loop. During this process, it's often necessary to clean up the output. As you can see, our file has some redundant whitespaces. The trimws() function is a handy tool to remove any leading or trailing whitespace. Let's see this in action:

  • Looping Over Lines: The for loop goes through each line in the lines vector.
  • Using trimws(): This function is applied to each line to remove any leading and trailing whitespace.

The output of this code will neatly display each line from data.txt. As you can see, there are no more leading whitespaces before "world":

Reading Integers from a File and Finding Their Sum

To extend our file line-by-line reading skills, let's look at an example where we read integers from a file and calculate their sum. Here's how you can do it:

Assume the numbers.txt file contains:

The following code reads integers from this file and calculates their sum. We convert each line to an integer using as.integer() and implement logic to calculate the sum.

  • Reading Lines: The file's lines containing numbers are read into the lines vector.
  • Converting to Integers: Each line is stripped of whitespace and converted to an integer using as.integer().
  • Calculating Sum: The converted integers are summed up in the total_sum variable.

After executing the code, the output will show the total sum of the numbers:

Summary and Practice Preparation

In this lesson, you gained the skills to read a text file line-by-line using R — a fundamental technique for processing large datasets efficiently. You've learned to manage file I/O operations safely and effectively with readLines(), explored how to utilize character vectors, and cleaned data with trimws().

Now, you're ready to dive into practice exercises where you can apply these concepts and strengthen your understanding. Continue to build on these skills as we explore further parsing techniques in future lessons. Keep up the great work!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal