Search code examples
rread.table

Read horizontal table into data frame in R


The .txt file is in the form:

high_quality 1.2 .9 .7 1.0 1.7 1.7 1.1 .9 1.7 1.9 1.3 2.1 1.6 1.8 1.4 1.3 1.9 1.6 .8 2.0 1.7 1.6 2.3 2.0

poor_quality 1.6 1.5 1.1 2.1 1.5 1.3 1.0 2.6 

high_quality line and poor_quality line have 1 blank line between them.

I have tried:

fabric_df = read.table(file="../data2/fabric.txt",sep = " ", header = TRUE)

But got the error:

"Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1 did not have 25 elements"

Does anybody know how to properly import this file into a data frame?


Solution

  • You can do data.table::fread("file.txt", fill=TRUE) which will give

        V1  V2  V3  V4  V5  V6  V7  V8  V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24
    1: 1.2 0.9 0.7 1.0 1.7 1.7 1.1 0.9 1.7 1.9 1.3 2.1 1.6 1.8 1.4 1.3 1.9 1.6 0.8   2 1.7 1.6 2.3   2
    2: 1.6 1.5 1.1 2.1 1.5 1.3 1.0 2.6  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA