Read TXT File with Spaces as Delimiter in R (Example)

 

In this tutorial you’ll learn how to import a text file with spaces as separator in the R programming language.

The article will consist of this information:

Let’s get started…

 

Creating Example Data

For this example, I have created a TXT file that looks as shown in the screenshot below:

 

space separator TXT

 

Note that between some values there is one space as separator and between other values there are multiple spaces as separator.

 

Example: Reading Text File with Multiple Spaces as Delimiter

The following code shows how to import a text file with a different number of spaces as delimiter using the read.table function.

Surprisingly, the answer is quite easy in R – Just don’t specify any separator within the read.table function. The R programming language knows by default how to handle multiple spaces.

Have a look at the following R code and its output:

data <- read.table("my file.txt",  # Read TXT file
           header = TRUE)
data                               # Print data in RStudio

 

table 1 data frame read txt file spaces as delimiter r

 

The previous table shows that our example data consists of four rows and three columns. The multiple spaces did not cause any problems when importing the data even though we didn’t specify the sep argument of the read.table function.

 

Video & Further Resources

Do you need more explanations on the topics of this article? Then you might want to have a look at the following video of my YouTube channel. In the video instruction, I show the R programming codes of this article:

 

 

In addition, you may want to have a look at the other R programming tutorials on this homepage:

 

To summarize: In this tutorial, I have illustrated how to read a TXT file with multiple spaces as delimiter in R programming. In case you have further comments or questions, tell me about it in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top