Importing Data from a Text File
We can easily import or read .txt file using basic R function read.table(). read.table() is used to read a file in table format. This function is easy to use and flexible.
Syntax:
# read data stored in .txt file
x<-read.table(“file_name.txt”, header=TRUE/FALSE)
R
# Simple R program to read txt file x<- read.table ( "D://Data//myfile.txt" , header= FALSE ) # print x print (x) |
Output:
V1 V2 V3
1 100 a1 b1
2 200 a2 b2
3 300 a3 b3
If the header argument is set at TRUE, which reads the column names if they exist in the file.
How To Import Data from a File in R Programming
The collection of facts is known as data. Data can be in different forms. To analyze data using R programming Language, data should be first imported in R which can be in different formats like txt, CSV, or any other delimiter-separated files. After importing data then manipulate, analyze, and report it.