0

I have a CSV file containing data with the first column in Unix time stamp. How can I convert it to xts form directly? Currently I am trying to read the file and convert using as.xts, but I get error messages every way I try.

An example of a code I used:

Data <- read.zoo("data.csv", index.column = 1, origin="01/01/1970",
                 sep = ",", header = TRUE, FUN = as.POSIXct)
as.xts(Data)

1st 2 lines of the csv:

1366930371  143.7   0.25275
1366930368  143.7   0.02664867
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418

1 Answers1

0

There could be several things wrong. First is that the first 2 lines of your "csv" are tab-separated, not comma-separated. Next, you specify header=TRUE, but the first 2 lines do not have a header. Third, origin= is in the wrong format. It should be yyyy-mm-dd.

This works:

library(xts)
Lines <- "1366978862,133.08,0.48180896
1366978862,133.08,0.5"
tc <- textConnection(Lines)
Data <- read.zoo(tc, sep=",", FUN=function(i) as.POSIXct(i, origin="1970-01-01"))
close(tc)
Data <- as.xts(Data)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418