Reading contents of a Text File in R Programming – read.table() Function
read.table()
function in R Language is used to read data from a text file. It returns the data in the form of a table.
Syntax:
read.table(filename, header = FALSE, sep = “”)Parameters:
header: represents if the file contains header row or not
sep: represents the delimiter value used in file
Example 1: Reading data from same directory
# R program to read a text file # Get content into a data frame data < - read.table( "TextFileExample.txt" , header = FALSE, sep = " " ) # Printing content of Text File print (data) |
Output:
V1 V2 V3 1 100 A a 2 200 B b 3 300 C c 4 400 D d 5 500 E e 6 600 F f
Example 2: Reading data from different directory
# R program to read a text file # Reading data from another directory # print x print (x) |
Output:
V1 V2 V3 1 100 a1 b1 2 200 a2 b2 3 300 a3 b3
Please Login to comment...