Read Fixed Width Text File in R

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.
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
# Creating fileabcd.txt<-tempfile()  cat(file=abcd.txt,"Rahul2023","Ravi 2521",    "Jaggu2130",sep="\n")  # Reading fixed widthread.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.
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()  # adding data to the empty tempfilecat(file = abcd.txt, "Rahul2023", "Ravi 2521",    "Jaggu2130", sep = "\n")  readLines(abcd.txt)unlink(abcd.txt)
Output:
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")  # format of character length=5, 2 # integers of 2-sized widthread.fortran(abcd.txt,c("A5","2I2"))  # format of 5 characters of width=1,4# integers of width-1read.fortran(abcd.txt,c("5A1","4I1"))  # using list of formats for each entry # of data for special casesread.fortran(abcd.txt,list(c("A5","2I2"),                           c("A4","X1","2I2"),                           c("A5","2I2")))  unlink(abcd.txt)
Output:
Picked
R-FileHandling
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Joining of Dataframes in R Programming
Control Statements in R Programming
How to import an Excel File into R ?
How to change Colors in ggplot2 Line Plot in R ?
Normal Distribution in R