The read.table() function used to read data from file or text into data frame.

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

Method 1: Use read.table() Function

read.table(file_path)

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

Use read.table() Function to Read Text Data from File

Suppose we want to read text data from sample.txt file which stored in current working directory:

# Read file into data frame
df <- read.table(file='sample.txt', header=TRUE)

# Print dataframe
print(df)

Output:

  Machine_name Pressure Temperature Status
1            A    78.20          31   TRUE
2            B    71.70          35   TRUE
3            C    80.21          36  FALSE
4            D    83.12          36  FALSE
5            E    82.56          38  FALSE
6            F    79.50          32   TRUE

As in the above code we read data from sample.txt file using read.table() function also we set header argument as TRUE to show data in correct format.