Reading contents of a Text File in R Programming - read.table() Function Last Updated : 25 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The read.table() function in R can be used to read a text file's contents. A versatile and often used function for reading tabular data from different file formats, including text files, is read.table(). 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 fileExample 1: Reading data from the same directory R data <- read.table("TextFileExample.txt", header = FALSE, sep = " ") # Printing content of Text File print(data) Output: V1 V2 V31 100 A a2 200 B b3 300 C c4 400 D d5 500 E e6 600 F fExplanation: The file "TextFileExample.txt" is read from the current directory. The header = FALSE argument states that there is no header row in the file, and sep = " " says that columns are separated by spaces.Example 2: Reading data from a different directory R # 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) Output: V1 V2 V31 100 a1 b12 200 a2 b23 300 a3 b3Reading contents of a Text File in R Programming - read.table() Function - FAQsCan we specify the column names while reading the text file?Yes, we can specify column names while reading the text file using the col.names parameter in the read.table() function. How can we skip header lines or ignore specific rows while reading the text file?If your text file contains header lines or rows that you want to skip while reading, you can use the skip parameter in the read.table() function. Specify the number of lines to skip as the value of the skip parameter. For example, to skip the first two lines, you can use.How to handle missing or incomplete values while reading?The read.table() function provides the na.strings parameter to handle missing or incomplete values. we can specify the character strings that should be treated asmissing values using this parameter. Comment More infoAdvertise with us Next Article Reading contents of a Text File in R Programming - read.table() Function N nidhi_biet Follow Improve Article Tags : R Language R Functions R-FileHandling Similar Reads Read contents of a CSV File in R Programming - read.csv() Function read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. The read.csv() function also accepts a number of optional arguments that we can use to modify the import procedure. we can choose to treat the first row as column names, sele 3 min read Read Lines from a File in R Programming - readLines() Function readLines() function in R Language reads text lines from an input file. The readLines() function is perfect for text files since it reads the text line by line and creates character objects for each of the lines. Syntax: readLines(path) Parameter: path: path of the file Example 1: Python3 # R progra 2 min read Create a Tabular representation of Data in R Programming - table() Function table() function in R Language is used to create a categorical representation of data with variable name and the frequency in the form of a table. Syntax: table(x) Parameters: x: Object to be converted Example 1: Python3 1== # R Program to create # a tabular representation of data # Creating a vecto 1 min read Scan and Read Data from a File in R Programming - scan() Function scan() function in R Language is used to scan and read data. It is usually used to read data into vector or list or from file in R Language. Syntax: scan("data.txt", what = "character") Parameter: data.txt: Text file to be scanned Returns: Scanned output Example 1: Scanning of Text r # R Program to 3 min read Convert an Object to a Table in R Programming - as.table() Function as.table() function in R Language is used to convert an object into a table. Syntax: as.table(x) Parameters: x: Object to be converted Example 1: Python3 1== # R Program to convert # an object to a table # Creating a vector vec = c(2, 4, 3, 1, 2, 3, 2, 1, 4, 2) # Calling as.table() Function as.table 1 min read Reading Tabular Data from files in R Programming Often, the data which is to be read and worked upon is already stored in a file but is present outside the R environment. Hence, importing data into R is a mandatory task in such circumstances. The formats which are supported by R are CSV, JSON, Excel, Text, XML, etc. The majority of times, the data 4 min read Contingency Tables in R Programming Prerequisite: Data Structures in R ProgrammingContingency tables are very useful to condense a large number of observations into smaller to make it easier to maintain tables. A contingency table shows the distribution of a variable in the rows and another in its columns. Contingency tables are not o 6 min read Reading Files in R Programming So far the operations using the R program are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. So the two most common o 9 min read Working with CSV files in R Programming CSV (Comma-Separated Values) files are plain text files where each row contains data values separated by commas or other delimiters such as tabs. These files are commonly used for storing tabular data and can be easily imported and manipulated in R. We will explore how to efficiently work with CSV f 3 min read Like