5

I am trying to read a text file which has columns separated by space, in R. I tried using data.table since read.csv is taking long to read. However, the first column has leading whitespace and I am getting the following error in fread().
"Not positioned correctly after testing format of header row. ch=' '"

The data format is similar to,

    45 36 46  
    45 67 35

Is there any way this can be read using fread() without reformatting the textile?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
DonDyck
  • 1,451
  • 5
  • 20
  • 35

2 Answers2

0

If you're under linux, try fread("sed 's/^[[:blank:]]*//' testing.dat"). The sed command will remove the leading blanks of every line in testing.dat.

cogitovita
  • 1,685
  • 1
  • 15
  • 15
0

This is a solution using readLines, but I am not sure about the speed.

require(data.table)

setwd("~")

in.df <- data.table(A = c(" a1"," b2"," c3"," d4"),
                    B = c(11,22,33,44))
in.df

write.table(in.df, file="testing.dat", quote = F,
            row.names = F, col.names = F)

dat <- paste(sub("^\\s+", "", readLines("testing.dat")), collapse = "\n")
dat

test.df <- fread(dat, stringsAsFactors = F)
test.df
djhurio
  • 5,437
  • 4
  • 27
  • 48