[R] Time and xts

robert denham rjadenham at gmail.com
Sun Jan 16 11:32:40 CET 2011


Assuming your csv file looks like:

Date,Time,Open,High,Low,Close
1/2/2005,17:05,1.3546,1.3553,1.3546,1.35495
1/2/2005,17:10,1.3553,1.3556,1.3549,1.35525
1/2/2005,17:15,1.3556,1.3556,1.35515,1.3553
1/2/2005,17:25,1.355,1.3556,1.355,1.3555

You could do something like:

aa <- read.csv("tmp.csv",header=TRUE,stringsAsFactors=FALSE)
aa$DateTime <- as.POSIXct(paste(aa$Date,aa$Time),format = "%d/%m/%Y %H:%M")

This gives you a column of POSIXct dates which might be useful for
your comparison. It leaves the Time column as text, so you should be
able to compare strings like in the code you presented, or you can
extract whatever you want from the dates using as.POSIXlt and
extracting the component:
names(as.POSIXlt(aa$DateTime[1]))


Robert


On Sun, Jan 16, 2011 at 2:20 PM, rnick <nikos.rachmanis at gmail.com> wrote:
>
> Hi all,
> I have run into a problem and some help would be highly appreciated.
> I have a .csv with the following columns:
> Date    Time    Open    High    Low     Close
> 1/2/2005        17:05   1.3546  1.3553  1.3546  1.35495
> 1/2/2005        17:10   1.3553  1.3556  1.3549  1.35525
> 1/2/2005        17:15   1.3556  1.35565 1.35515 1.3553
> 1/2/2005        17:25   1.355           1.3556  1.355           1.3555
> ….
> …..
> 2/13/2006       5:20    1.18895 1.18925 1.18835 1.1885
>
> 1)      Without using zoo, xts or any other time series object, I am trying to
> run this code but fails to pass the argument to the function
>
> #n is the length of the series
>
> for (t in seq(from=10,to=n,by=1))
> {
>        while (time[t]=='02:00:00')
>        {
>                entrytrade(t)
>        }
> }
> Is this possible to do? If yes, any ideas what I am doing wrong?
>
> 2)      I have also tried with xts by creating the object like this
> lines<-data.frame(date,time,open,high,low,close)
> z <- read.zoo(lines, header = TRUE, index = list(1, 2), FUN = function(d,t)
> as.POSIXct(paste(date,time), format = "%m/%d/%Y %H:%M"))
> x<-as.xts(z)
>
> However, I am unable to call just the time and run the above for loop. any
> ideas here?
>
> Thanks
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Time-and-xts-tp3219722p3219722.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list