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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) 

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to read a text file
  
# Reading data from another directory 
x<-read.table("D://Data//myfile.txt", header = FALSE) 
    
# print x 
print(x) 

chevron_right


Output:

   V1 V2 V3
1 100 a1 b1
2 200 a2 b2
3 300 a3 b3
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.