Reputation: 9061
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?
Upvotes: 1
Views: 979
Reputation: 118889
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.
Upvotes: 1