If you want to read the .csv file directly into a time series object, you can use the function read.zoo()
from the zoo
package. This internally calls read.table()
(rather than read.csv
) and then converts the specified time index column(s). See ?read.zoo
and vignette("zoo-read", package = "zoo")
.
An example with time stamps like yours is:
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 yields a zoo
series with POSIXct
time stamps:
x y
2014-12-01 18:54:22 0 1
2014-12-01 19:43:11 1 2
(Of course, the text = csv
would have to be replaced by something like file = "my.csv"
if you are reading a .csv file from the disk rather than a text string from within R.)