[R] calculate within-day correlations
Joshua Ulrich
josh.m.ulrich at gmail.com
Sat Sep 15 14:35:59 CEST 2012
On Fri, Sep 14, 2012 at 6:14 PM, emorway <emorway at usgs.gov> wrote:
> Hi Joshua,
>
> I was attempting to work with your code on the larger datasets, which I have
> to read in with 'read.table', but I've apparently missed something. When I
> attempt to massage the data.frame a bit in the code below, as.POSIXct drops
> the time component which then precludes use of xts. I think you'll see what
> I mean (the rdb file is a text file containing the data, which is attached
> to the post):
>
> library(xts)
> Q_hourly<-read.table("C:/temp/07130500_BelowJM_q.rdb",skip=59,col.names=c('date','time','tz','Q','rating','unknown'),colClasses=c("character","character","character","numeric","character","character"))
>
> notice that
>
> Q_hourly[1,]
> # date time tz Q rating unknown
> #1 19981001 000000 MDT 326 3 A
> Q_hourly[2,]
> # date time tz Q rating unknown
> #2 19981001 001500 MDT 326 3 A
>
> and
>
> paste(strptime(Q_hourly[1,1],"%Y%m%d"),"
> ",format(strptime(Q_hourly[1,2],"%H%M%S"),format="%H:%M:%S"),sep='')
>
> #[1] "1998-10-01 00:00:00"
>
> but for some reason, the time stamp is dropped in the following, which
> breaks the call to xts (I think)
>
> as.POSIXct(paste(strptime(Q_hourly[1,1],"%Y%m%d"),"
> ",format(strptime(Q_hourly[1,2],"%H%M%S"),format="%H:%M:%S"),sep=''),format="%Y-%m-%d
> %H:%M:%S",tz="")
>
> #[1] "1998-10-01 PDT"
>
This is just how the POSIXct objects are *printed*. It doesn't affect
how they're actually stored.
> The Q_use data.frame I'm building here should come out exactly
> http://r.789695.n4.nabble.com/file/n4643206/07130500_BelowJM_q.rdb
> 07130500_BelowJM_q.rdb the same as in my original post (above), but I can't
> seem to seem to force the preservation of the time stamp even though I've
> explicitly stated the format I want to be stored (...format="%Y-%m-%d
> %H:%M:%S"). Any ideas?
>
> xQ <- xts(Q_use["Q"], Q_use$date)
> #Error in `[.data.frame`(x, indx) : undefined columns selected
>
I don't know how you created the Q_use object, so I don't know what
the problem is with the above line of code. This works for me:
Q_hourly$datetime.str <- paste(Q_hourly$date, Q_hourly$time)
fmt <- "%Y%m%d %H%M%S"
x <- xts(Q_hourly["Q"], as.POSIXct(Q_hourly$datetime.str, format=fmt))
Best,
--
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com
>
> --
> View this message in context: http://r.789695.n4.nabble.com/calculate-within-day-correlations-tp4643091p4643206.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