1

I'm learning to use data.table. Here is some sample text I'd like to read into a data.table:

Date           Col1       Col2
2014-01-01     123        12
2014-01-01     123        21
2014-01-01     124        32
2014-01-01     125        32
2014-01-02     123        34
2014-01-02     126        24
2014-01-02     127        23
2014-01-03     521        21
2014-01-03     123        13
2014-01-03     126        15

read.table works fine here:

df <- read.table("dat", header=T)

When I try to use fread() from data.table package, I got the error:

DT <- fread("dat",header=T)
Error in fread("dat", header = T) : 
  Not positioned correctly after testing format of header row. ch=' '

I also tried to set sep:

DT <- fread("dat",header=T, sep="\t")

However, the structure is not right: It's only 1 variable $Date (it should be 3).

str(DT)
Classes 'data.table' and 'data.frame':  10 obs. of  1 variable:
 $ Date           Col1       Col2: chr  "2014-01-01     123        12" "2014-01-01     123        21" "2014-01-01     124       

How to use fread() here?

Community
  • 1
  • 1
Nick
  • 8,451
  • 13
  • 57
  • 106
  • this answer may help http://stackoverflow.com/questions/22229109/r-data-table-fread-command-how-to-read-large-files-with-irregular-separators – bjoseph Aug 07 '15 at 14:24
  • @bjoseph Thanks! Yes, `sed` works. Since `read.table` can handle the data perfectly, I suppose `fread` should handle it as well. If the data set is very large, the `sed` approach may also take some time. Maybe `fread` is a better choice if we could get it working. – Nick Aug 07 '15 at 14:33
  • agreed. there are some github issues about this https://github.com/Rdatatable/data.table/issues/964 – bjoseph Aug 07 '15 at 14:42

1 Answers1

1

As said in this post, this is now handled automatically by fread (see README for more):

require(data.table) #v1.9.5+
fread("~/Downloads/tmp.txt")
#           Date Col1 Col2
#  1: 2014-01-01  123   12
#  2: 2014-01-01  123   21
#  3: 2014-01-01  124   32
#  4: 2014-01-01  125   32
#  5: 2014-01-02  123   34
#  6: 2014-01-02  126   24
#  7: 2014-01-02  127   23
#  8: 2014-01-03  521   21
#  9: 2014-01-03  123   13
# 10: 2014-01-03  126   15

Either update to latest devel version or wait until v1.9.6 hits CRAN.

Community
  • 1
  • 1
Arun
  • 116,683
  • 26
  • 284
  • 387