Read Fixed Width Text File in R
Last Updated :
17 Jun, 2021
In this article, we are going to see how to read fixed-width text files in R Programming language.
In text files, columns will have fixed widths, specified in characters, which determines the maximum amount of data it can contain. No delimiters are used to separate the fields in the file. Instead, smaller quantities of data are padded with spaces to fill the allotted space, such that the start of a given column can always be specified as an offset from the beginning of a line.
There are many methods to read the data in fixed width text file:
- Using read.fwf( ) function
- Using readLines( ) function.
- Using Fortran style format specification.
Method 1: Using read.fwf( ) function.
This method is done using read.fwf function from utils package. We have to use column widths for reading.
Syntax: read.fwf(file, widths, header = FALSE, sep = “\t”, skip = 0, row.names, col.names, n = -1, buffersize = 2000, fileEncoding = “”, …)
Now, we use read.fwf() for reading the fixed-width text file named “abcd.txt”.
R
abcd.txt<- tempfile ()
cat (file=abcd.txt, "Rahul2023" , "Ravi 2521" ,
"Jaggu2130" ,sep= "\n" )
read.fwf (abcd.txt, width = c (5, 2, 2),
col.names = c ( "Studentname" , "Test1" ,
"Test2" ))
unlink (abcd.txt)
|
Output:
Here we take widths in (5,2,2) format as student names are in 4,5 string length and marks are in 2-sized string length.

In the above output, we can observe that it reads the “abcd.txt” file and displays in format of 5,2,2 lengths(example: rahul,20,23 from “rahul2023”), with column names as Studentnames,Test1,Test2.
Method 2: Using readLines() function.
Here we are going to use 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
Code:
R
abcd.txt<- tempfile ()
cat (file = abcd.txt, "Rahul2023" , "Ravi 2521" ,
"Jaggu2130" , sep = "\n" )
readLines (abcd.txt)
unlink (abcd.txt)
|
Output:

Method 3: Using Fortran style format specification.
Here we will use read.fortran() function. It is used to read fixed-format data files using Fortran-style format specifications
Syntax: read.fortran(file, format, …, as.is = TRUE, colClasses = NA)
Parameters:
- file: File or connection to read from.
- format: Character vector or list of vectors. See ‘Details’ below.
- as.is: Keep characters as characters?
- colClasses: Variable classes to override defaults.
Here we have created a file named “abcd.txt” for example purpose as shown in the below code. By using read.fortran function, we can read the data in fixed-width text file using fortran style format specifications.
R
abcd.txt<- tempfile ()
cat (file = abcd.txt, "Rahul2023" ,
"Ravi 2521" , "Jaggu2130" , sep = "\n" )
read.fortran (abcd.txt, c ( "A5" , "2I2" ))
read.fortran (abcd.txt, c ( "5A1" , "4I1" ))
read.fortran (abcd.txt, list ( c ( "A5" , "2I2" ),
c ( "A4" , "X1" , "2I2" ),
c ( "A5" , "2I2" )))
unlink (abcd.txt)
|
Output:

Similar Reads
Read text File with Space as Delimiter in R
In this article, we will discuss how to read a text file with spaces as delimiters in R programming language. Base R allows us to read and access the content within the text files with any number of characters as the delimiter. File in use: The read.table() method in R can be used to read data from
2 min read
How to Read Text Files with Pandas?
In this article, we will discuss how to read text files with pandas in Python. In Python, the Pandas module allows us to load DataFrames from external files and work on them. The dataset can be in different types of files. Text File Used Read Text Files with PandasBelow are the methods by which we c
6 min read
R Read Text File to DataFrame
In today's data-driven world, collecting data from multiple sources and turning it into a structured manner is a critical responsibility for data analysts and scientists. Text files are a prominent source of data, as they frequently include useful information in plain text format. To be used success
5 min read
Reading a Text File With SQL Server
Here we will see, how to read a text file with SQL Server. We can read the text file using the OPENROWSET(BULK ) function. OPENROWSET(BULK) It is a table-valued function that can read data from any file. This function returns a single column table having all the contents of the file. This single lar
2 min read
How to Read Many Files in R with Loop?
When working with data in R Programming Language you often need to read multiple files into your environment. If you have a large number of files, doing this manually is impractical. Instead, you can use a loop to automate the process. This guide will show you how to read many files in R using a loo
4 min read
How to Read Zip Files into R
In the R Programming Language Zip files are compressed archives that store one or more files or directories in compressed format. They are commonly used to package and distribute files, particularly when working with huge datasets or many files. Zip files not only conserve disc space but also facili
4 min read
How to Read XML File in R?
XML (Extensible Markup Language) can be a widely used format for storing and transporting data. It can be structured and allowing the both humans and machines to easily parse and interpret the data it contains. In R programming, reading and processing XML files is straightforward thanks to the vario
5 min read
Read xlsb files in R
A XLSB file is an Excel binary worksheet file that was created by Microsoft. In contrast to other Excel files, they hold information in binary format. The fact that XLSB files are binary means that they can be read and written much more quickly, which makes them helpful for large spreadsheets. In th
2 min read
Add New Line to Text File in R
In this article, we are going to add a new line to the Text file using the R Programming language. It can be done in different ways: Using write() function.Using lapply() function.Using cat() function. Method 1: Adding text using write() function. Now we are going to select the line which is going t
3 min read
Text Mining in R with tidytext
Text mining, also known as text data mining or text analytics, involves extracting useful information and patterns from text data. The tidytext package in R provides a set of tools to help transform and analyze text data in a tidy format. This article will introduce the fundamental concepts of text
4 min read