Skip to content

Help converting file to XTS

4 messages · Johannes Møllerhagen, Bert Gunter, David Winsemius +1 more

#
Hello there! I am a master student working on my master thesis, and I am trying to convert some data to xts so I can apply a highfrequency package to it.

At the moment I am trying to use a POSIXct function. I am quite new at this program and I am having some issue. The file is  attached.


The current coding is:


dat<-read_csv("TEL5minint.csv")
xts(dat,order.by=as.POSIXct(dat),"%d/%m/%Y %H:%M")


And the error is:

Error in as.POSIXct.default(dat) :
  do not know how to convert 'dat' to class ?POSIXct?


Any help is appreciated!


Kind Regards

Johannes
#
Hard to say without knowing what dat looks like.

Can you show us a small sample, perhaps via dput( head( dat))  ?
See ?dput, ?head for details.

A guess would be that dat is a data frame and not a character string, but
????


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Feb 5, 2019 at 9:24 AM Johannes M?llerhagen <johamol at stud.ntnu.no>
wrote:

  
  
#
On 2/5/19 6:06 AM, Johannes M?llerhagen wrote:
So dat is now a dataframe
And you are supplying a list (which is what dataframes are) to the 
order.by parameter.
The function is telling you that dat is the wrong type of object to be 
converted to the rownames of a matrix (which is what xts objects are.
#
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: