The collection of facts is known as data. Data can be in different forms. To analyze data using R, data should be first imported in R which can in different formats like txt, CSV, or any other delimiter separated files. After import data then manipulate, analyze, and report it.
Importing Data from a Text File
We can easily import or read .txt file using basic R function
read.table() is used to read a file in table format. This function is easy to use and flexible.
# read data stored in .txt file x<-read.table("file_name.txt", header=TRUE/FALSE)
V1 V2 V3 1 100 a1 b1 2 200 a2 b2 3 300 a3 b3
X100 a1 b1 1 200 a2 b2 2 300 a3 b3
If the header argument is set at TRUE, which read the column names if they exist in the file.
Importing Data from a CSV file
CSV(Comma Separated Values) file contains list of data which separated from comma(, ) .To import csv file R uses
read.csv2() function. Both function are almost same as to the
# import csv file using read.csv() x <- read.csv("file_name.txt")
X V1 V2 V3 1 1 100 a1 b1 2 2 200 a2 b2 3 3 300 a3 b3
# import csv file using read.table() x <- read.table("file_name.txt", header = TRUE, sep="")
Col1.Col2.Col3 1 100, a1, b1 2 200, a2, b2 3 300, a3, b3
Importing Data from a delimited file
R has a function
read.delim() to read delimited file in to list. File is by default separated by tab which is represented by sep="", that separated can be comma(, ), dollar symbol($) etc.
# import delimited file x <- read.delim("file_name.txt", sep="", header=TRUE)
X.V1.V2.V3 1 1, 100, a1, b1 2 2, 200, a2, b2 3 3, 300, a3, b3  "list
V1 1, V1, V2, V3 2 1, 100, a1, b1 3 2, 200, a2, b2 4 3, 300, a3, b3  "list
- Scan and Read Data from a File in R Programming - scan() Function
- Data Wrangling in R Programming - Data Transformation
- Convert an Object to Data Frame in R Programming - as.data.frame() Function
- Check if the Object is a Data Frame in R Programming - is.data.frame() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Modify Data of a Data Frame with an Expression in R Programming - with() Function
- Generate a set of Sample data from a Data set in R Programming - sample() Function
- Printing out to the Screen or to a File in R Programming - cat() Function
- Read contents of a CSV File in R Programming - read.csv() Function
- Reading contents of a Text File in R Programming - read.table() Function
- Read Lines from a File in R Programming - readLines() Function
- File Handling in R Programming
- Data Handling in R Programming
- Data Reshaping in R Programming
- Data Structures in R Programming
- R Programming for Data Science
- Reading Tabular Data from files in R Programming
- Accessing variables of a data frame in R Programming - attach() and detach() function
- Comparing values of data frames in R Programming - all_equal() Function
- Convert type of data object in R Programming - type.convert() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.