Jesus
Jesus

Reputation: 462

How can I read with R data.table a csv with dates

How can I read perfectly this kind of CSV that has dates, using data.table library?

date
2011-12-31T12:00:00Z
2011-12-31T13:00:00Z
2011-12-31T14:00:00Z
2011-12-31T15:00:00Z
2011-12-31T16:00:00Z
2011-12-31T17:00:00Z
2011-12-31T18:00:00Z
2011-12-31T19:00:00Z

The issue that I have is that is not recognizing date format:

Warning message in fread: Starting data input on line 2 and discarding line 1 because it has too few or too many items to be column names or data: date

I use to read it:

data<-fread(data.csv,header=T)

And the data.table returned is:

2011-12-31T12   00  00Z
2011-12-31T13   0   00Z
2011-12-31T14   0   00Z
2011-12-31T15   0   00Z
2011-12-31T16   0   00Z
2011-12-31T17   0   00Z
2011-12-31T18   0   00Z 

Thanks!

Upvotes: 1

Views: 5395

Answers (2)

Jesus
Jesus

Reputation: 462

I have found the solution:

data<-fread("data.csv",sep=",")

Upvotes: 0

MichaelChirico
MichaelChirico

Reputation: 34703

You can try updating your installation to the development version of data.table (1.10.5+); the sep detection logic has improved a bit of late:

# if this doesn't work for you, check the Installation page on GitHub
install.packages('data.table', type = 'source',
                 repos = 'http://Rdatatable.github.io/data.table')

With that your file reads without any other input:

fread('date
2011-12-31T12:00:00Z
2011-12-31T13:00:00Z
2011-12-31T14:00:00Z
2011-12-31T15:00:00Z
2011-12-31T16:00:00Z
2011-12-31T17:00:00Z
2011-12-31T18:00:00Z
2011-12-31T19:00:00Z
')
#                    date
# 1: 2011-12-31T12:00:00Z
# 2: 2011-12-31T13:00:00Z
# 3: 2011-12-31T14:00:00Z
# 4: 2011-12-31T15:00:00Z
# 5: 2011-12-31T16:00:00Z
# 6: 2011-12-31T17:00:00Z
# 7: 2011-12-31T18:00:00Z
# 8: 2011-12-31T19:00:00Z

Upvotes: 1

Related Questions