[R] Help converting file to XTS

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Tue Feb 5 19:39:14 CET 2019


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 using stud.ntnu.no> wrote:
>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
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list