Data.table fread how to ignore empty string

It appears that if the second line in the file is empty, the column names will not be read. I played with switches such as header, skipped from the documentation, but can't make it work.

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

second line is empty:

> fread('c1 c2\n\n1 2\n3 4\n') V1 V2 1: 1 2 2: 3 4 

second line is not empty:

 > fread('c1 c2\n1 2\n3 4\n') c1 c2 1: 1 2 2: 3 4 
+6
source share
2 answers

The current version of data.table (1.9.8+) adds an argument to blank.lines.skip , which seems to solve this problem:

 fread('c1 c2\n\n1 2\n3 4\n',blank.lines.skip = TRUE) # c1 c2 # 1: 1 2 # 2: 3 4 
+5
source

Workaround: read the file with fread , then use another tool to correctly read only the first row to add it to the column names.

 library(data.table) library(readr) str_data <- 'c1 c2\n\n1 2\n3 4\n' dt <- fread(str_data) df.header <- read_delim(str_data, delim = " ", n_max = 0) setnames(dt, names(df.header)) > dt c1 c2 1: 1 2 2: 3 4 
+2
source

All Articles