Skip to content
Prev 378451 / 398502 Next

Help converting file to XTS

One thing about POSIXct or POSIXlt... you always need to address the issue of timezone. If your timestamp data are simple (no daylight savings) you may be able to get away with a simple

Sys.setenv( TZ="GMT" )

sometime in each R session prior to converting anything to this type (e.g at the beginning of your script).

You should read the help pages for read_csv

?read_csv

to find out that the object created by that function is a tibble (a variety of data frame). Then read

?xts

to find out that the order.by must be a time-based class (which requirement POSIXct meets), and then

?as.POSIXct

which sadly only says that x has to be an R object. There are a variety of specializations for this function, e.g. as.POSIXct.Date, as.POSIXct.numeric, as.POSIXct.POSIXlt, and as.POSIXct.default, the last of which handles conversion from character or factor data types. Note that none of these options include converting an entire data frame.

Since dat is a tibble you will need to use $ or `[[` indexing to extract the one column that contains the timestamps:

dat[[ 1 ]]

and convert it

as.POSIXct( dat[[ 1 ]], format="%d/%m/%Y %H:%M" )

and use that as the order.by argument

result <- xts( dat[-1] ,order.by= as.POSIXct( dat[[ 1 ]], format="%d/%m/%Y %H:%M" ) )

[1] https://stackoverflow.com/questions/9327700/read-data-from-excel-to-r-and-convert-to-xts
On February 5, 2019 6:06:15 AM PST, "Johannes M?llerhagen" <johamol at stud.ntnu.no> wrote: