How to read csv with time field?

I am trying to import a csv file with a field Ts

containing ISO8601 timestamp values ​​(e.g. 2014-12-01T18: 54: 22.973 + 0000).

I saw that you can specify the column class:

kd <- read.csv( "my.csv", colClasses=c( "Ts"="?"  ))

      

However, I cannot find how to declare the timestamp field.

Question: How can I indicate that this field is a timestamp?

+3


source to share


2 answers


If you want to read the CSV file directly into the time series object, you can use the function read.zoo()

from the package zoo

. This internally calls read.table()

(rather than read.csv

) and then converts the specified time index column. See ?read.zoo

and vignette("zoo-read", package = "zoo")

.

Example with timestamps like yours:

csv <-
"x,y,timestamp
0,1,2014-12-01T18:54:22.973+0000
1,2,2014-12-01T19:43:11.862+0000"
read.zoo(text = csv, sep = ",", header = TRUE, index = "timestamp",
  format = "%Y-%m-%dT%H:%M:%OS%z", tz = "GMT")

      



And this gives a series zoo

with timestamps POSIXct

:

                    x y
2014-12-01 18:54:22 0 1
2014-12-01 19:43:11 1 2

      

(Of course, text = csv

needs to be replaced with something like file = "my.csv"

if you are reading a CSV file from disk and not a text string from R.)

+4


source


Not sure how to do this directly while reading it, but as a workaround (until someone becomes more knowledgeable) you can make translated words:



kd <- read.csv("my.csv")
%  Assume that the timestamp column in the csv file has the header 'timestamp'

kd$newtimestamp <- strptime(kd$timestamp,format="%FT%H:%M:%OS%z")

%  By default this will convert all times to your timezone 
%  but you can control the conversion through the tx argument e.g. tx='GMT' 

      

+2


source







All Articles