'fread' is not compatible with leading / trailing spaces?

I am trying to read a text file that has space separated columns in R. I tried to use data.table as read.csv takes a long time to read. However, the first column has leading spaces and I am getting the following error in fread ().
"Not correctly set after testing the header line format. Ch = ''"

The data format is the same as

    45 36 46  
    45 67 35

      

Can this be read using fread () without reformatting the textiles?

+3


source to share


3 answers


# This example reproduces the error
library(data.table)
in.df <- data.frame(A=c(" a1"," b2"," c3"," d4"), B=c(11,22,33,44),
                    stringsAsFactors=FALSE)
write.table(in.df, file="testing.dat", quote=FALSE, row.names=FALSE, col.names=FALSE)
test.df <- fread(input="testing.dat", sep=" ", header=FALSE, stringsAsFactors=FALSE,
                 verbose=TRUE)

# I can't find a solution in ?fread

      



0


source


If you're on Linux, give it a try fread("sed 's/^[[:blank:]]*//' testing.dat")

. The command sed

removes leading spaces from each line in testing.dat

.



0


source


This is the solution using readLines

, but I'm 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

      

0


source







All Articles