How to import text files to R as DataFrame with Tidyverse?

Read txt files with Tidyverse

Step 1: Import the readr package

First off, make sure that the Tidyverse readr package is installed in your RStudio (or Jupyter) dev environment , and install it if required:

if (!require(readr)) {
  install.packages("readr")
}

Next import the readr library for using t in your development environment:

library ("readr")

Step 2: Define path to text / csv file

Next define a path to the file you would like to import. The path could be either in your local file system or a remote server location.

In our case we will import some simple course related information from the local directory.

txt_path = "C:\\Temp\\Courses_Data.csv" 

Step 3: Import text file as DataFrame

We can now import the text file as a DataFrame:

courses_data <- read_csv(txt_path, col_names = TRUE)

Note: the col_names attribute signals that the file has column headers. If that’s not the case, make sure to set col_names=FALSE.

Note: You can browse your DataFrame content from the Environment tab in RStudio.

Import text and csv files with R base

Use the following procedure for reading the contents of a text file line by line into a DataFrame:

Step 1: Define path to csv/txt file

As shown above, the first step is to define the file that we would like to import into our R development environment.

txt_path = "C:\\Temp\\Courses_Data.csv" 

Step 2: Read file as DataFrame

Next step is to invoke the R base read.csv function to get the text file contents into a DataFrame.

courses_data <- read.csv (txt_path)

We can easily verify that courses_data is a DataFrame object by using the following function:

is.data.frame(courses_data)

This will return the value TRUE in the console.

Get text with read.table

An alternative option to get rectangular data stored as txt or csv into R is using the read.table method:

courses_data <- read.table (txt_path, header = TRUE, sep =",", dec=".")

Note that in this case, you need to specify the delimiting character (sep) that is used in your file as well the decimal separator (dec)

Additional Learning

How to combine one or multiple R DataFrame columns?