Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table fread how to ignore empty line [duplicate]

Tags:

r

data.table

It looks like if the second line in the file is empty, the column names will not be read. I've played with switches such as header, skip, from the documentation but can't get it to work.

In case the second line in my file is empty, how to ignore this fact and still read the first line as column names?

the second line is empty:

> fread('c1 c2\n\n1 2\n3 4\n')

   V1 V2
1:  1  2
2:  3  4

the second line is not empty:

> fread('c1 c2\n1 2\n3 4\n')
   c1 c2
1:  1  2
2:  3  4
like image 897
Jim Green Avatar asked Dec 31 '15 00:12

Jim Green


1 Answers

The current version of data.table (1.9.8+) adds a blank.lines.skip argument that seems to resolve this problem:

fread('c1 c2\n\n1 2\n3 4\n',blank.lines.skip = TRUE)
#    c1 c2
# 1:  1  2
# 2:  3  4
like image 138
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 28 '22 14:09

A5C1D2H2I1M1N2O1R2T1