How To Read Text File Without Delimiter In Separate Rows And Columns

Hi All,

I have a raw text file that I need to read which doesn't have any delimiter or spaces between two columns. I want to read each line such that I can make a data frame that has rows and column equal to the number of rows and column in the text file.

Data

test <- read.table(text = "ABC
           DEF123
           DEF123-4
           DEF12345.PQR
           ..................................
           ..................................
           ..................................
           .............AAAAAAAA.............
           ............AAAAAAAAAA............
           ............AAAAAAAAAA............
           ...........AAAAAAAAAAAA...........
           ..........AAAAAAAAAAAAAA..........
           ..........AAAAAAAAAAAAAA..........
           .........AAAAAAAAAAAAAAAA.........
           .........AAAAAAAAAAAAAAAA.........
           ..................................
           ..................................")

Above data is in a text file and when I read it using read.table() or read.delim(), I get same 17 rows as above but 1 column, but I want to have 17 rows but with 34 columns with each cell holding each of the value above.

Any suggestions how I can solve this issue?

Thanks.

Hi @chetanpatil! If you want to read in a file where there are no delimiters or whitespace, you'll probably want to use a fixed-width file reading function. These functions assume each column is of a specified width (n characters wide).

For example, read.fwf() has a widths parameter, which is a vector stitching the number of characters wide each column is (I'm not sure if it recycles if you give it a single-element vector).

I hope that helps!

3 Likes