[R-SIG-Finance] Problems with time format and read.csv()
Joshua Ulrich
josh.m.ulrich at gmail.com
Mon Sep 17 20:18:52 CEST 2012
Use the colClasses arg to read Date and Time as character:
dat <- read.csv("tick.csv", colClasses=c(rep("character",2),rep("numeric",6)))
HTH,
--
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com
On Mon, Sep 17, 2012 at 1:14 PM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
> Hello,
>
> Following a previous post,
> http://www.mail-archive.com/r-sig-finance@r-project.org/msg03727.html
>
> I am having trouble with the hours format as it is picked up by the
> read.csv() command.
>
> The following data:
>
> "Date","Time","Open","High","Low","Close","Up","Down"
> 04/27/2012,0930,12.24,12.24,12.24,12.24,0,300
> 04/27/2012,0930,12.24,12.24,12.24,12.24,0,200
>
> are read as
>
> datain<-read.csv('tick.csv', header=TRUE)
>
>> head(dat)
> Date Time Open High Low Close Up Down
> 1 04/27/2012 930 12.24 12.24 12.24 12.24 0 300
> 2 04/27/2012 930 12.24 12.24 12.24 12.24 0 200
>
>
> i.e., 0930 becomes 930. How can this be translated to a "proper" time i.e,
> 9:30 so I can convert the data frame to an XTS object accordingly?
>
> I try to fix it by using %k below instead of %H (which also didn't work)
>
> hf<-xts(dat[,6], as.POSIXct(paste(dat[,1], dat[,2]), format = "%m/%d/%Y
> %k%M"))
>
> but I get NAs
>
>> head(hf)
> [,1]
> <NA> 12.24
> <NA> 12.24
> <NA> 12.25
> <NA> 12.25
> <NA> 12.25
> <NA> 12.25
>
> using %I in the format= above also didn't work. I gather that this is not a
> timezone problem and has to do with the way the time format is
> understood by R when generating a time index for the XTS object.
>
> Thanks in advance for your time and answers,
> Costas
> --
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
More information about the R-SIG-Finance
mailing list