To import data from text file into R you can use read.csv() function. In this you need to pass text file path to this function.

The following method shows how you can do it with syntax.

Method: Use read.csv() Function

read.csv(path of text file\\file_name.txt)

The following example shows how to use read.csv() function in R.

Using read.csv() Function to Import Text File

Suppose I want to read machine.txt file then use read.csv() function.

Let’s see example how to use this syntax to import text file:

# Read text file
df = read.csv("machine.txt")

# Print text data


Start_date Machine_name Value Reading
1 2000-05-21     Machine1   110     110
2 1998-03-28     Machine2   120      97
3 2001-01-19     Machine3   108      95
4 2003-04-27     Machine4    95      89
5 2004-11-26     Machine5    98      80
6 2008-11-25     Machine6   105      85

The output shows data from machine.txt file.

By following this syntax you can successfully import text file.