Hello, readers! In this article, we will be focusing on one of the interesting topics in R programming – how to read files in R.
So, let us get started!
When it comes to handling files, we face situations where we are required to read from within the file in a customized format. That is reading line by line or entire text at once or into a row-column format and so on.
To suffice all the above such requirements, R provides us with some basic file handling operations with respect to reading data from a file.
We will be making use of the below text file as a reference in all the examples mentioned in the course of this article.
We would be covering the below functions as a part of the course of this topic.
- read.delim() function
- read_tsv() function
- read_lines() function
- read_file() function
Table of Contents
1. Read files in R with the read.delim() function
R provides us with
read.delim() function to read the data values from a file at ease.
With read.delim() function, we can read the content of a text file in a tab separated format. That is, read.delim() function understands tab separated i.e. text format of data.
- file: The exact file name
- header: If set to TRUE, it considers the first row of the file as header columns for the data. To avoid that, set the header to FALSE.
rm(list = ls()) getwd() info = read.delim('info.txt',header = FALSE) print(info)
It has considered the three lines of the file as three rows and has returned them along with the default index values as shown below-
1 Welcome to the concept of File Handling in R programming! 2 Hello folks! 3 Welcome to R programming with Journaldev.
2. Reading data using readr library
R provides us with ‘readr‘ library to read the tab separated values easily. It has
read_tsv() function that reads the tab separated values individually.
library(readr) read_tsv(file, col_names)
- col_names: If set to TRUE, it considers the first row as the header for columns.
In this example, we have read the contents of a file using read_tsv() function and have set col_names to False.
rm(list = ls()) getwd() library(readr) info = read_tsv('info.txt',col_names = FALSE) print(info)
Thus, as a result, it returns the data into a row-column format wherein it considers a single row of data as one row of data.
# A tibble: 3 x 1 X1 <chr> 1 Welcome to the concept of File Handling in R programming! 2 Hello folks! 3 Welcome to R programming with Journaldev.
3. R read_lines() function to read file
As stated in the beginning, R provides us with functions to read the data in a customized manner.
read_lines() function, we can read the data in an user defined format i.e. read specific chosen lines from a file. We just need to pass the number of the lines which we wish to read from the file.
read_lines(file, skip, n_max)
- file: The name of the file to read data from.
- skip: The number of lines to skip prior to reading the data. Default value = 0
- n_max: The number of lines to read data from the file.
rm(list = ls()) getwd() library(readr) info = read_lines('info.txt',n_max = 2) print(info)
In this example, we have passed n_max = 2 i.e. the read_lines() function reads only the first two lines from the file.
> print(info)  "\"Welcome to the concept of File Handling in R programming!\""  "Hello folks!"
4. Reading data using R read_file() function
In order to get the entire data in a single go, we use read_file() function. With
read_file() function, we can fetch and read the entire data present in the file at once.
rm(list = ls()) getwd() library(readr) info = read_file('info.txt') print(info)
"\"Welcome to the concept of File Handling in R programming!\"\r\nHello folks!\r\nWelcome to R programming with Journaldev."
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to R programming, Stay tuned with us.
Till then, Happy Learning! 🙂