[R-SIG-Finance] [R-sig-finance] xts and barChart (quantmod)

Jeff Ryan jeff.a.ryan at gmail.com
Thu Jul 24 18:16:51 CEST 2008


The issue is with your original dji object.

The first column of you data.frame is forcing the zoo/xts matrix to be
a character one.

Try:

dji2 <- xts( dji[,-1], order.by= as.POSIXct( dji[,1])
colnames(dji2)<-c('Open', 'High', 'Low', 'Close', 'Volume')

The only issue remaining is that your original data.frame may be all
characters, though I assume that is not the case here.

Jeff (with input from Josh)

On Thu, Jul 24, 2008 at 10:20 AM, kafkaz <kafka at centras.lt> wrote:
>
> My data is stored in a MySQL database. So, I'm using RMySQL to load
> information about price performance. After that, I'm using xts to convert
> selected data into XTS object. Example:
>
> rs <- dbSendQuery(con, "select ....");
> dji<- fetch(rs, n = -1)
> dji2<-xts(dji,order.by=as.POSIXct(dji[,1]))
> colnames(dji2)<-c('Datetime', 'Open', 'High', 'Low', 'Close', 'Volume')
> At this point I have a xts object:
>
> class(dji2)
> [1] "xts" "zoo"
>
>  >str(dji2)
> An 'xts' object from 2004-01-07 15:50:00 to 2004-01-09 11:45:00 containing:
>  Data: chr [1:50, 1:6] "2004-01-07 15:50:00" "2004-01-07 15:55:00"
> "2004-01-07 16:00:00" "2004-01-07 16:05:00" ...
>  - attr(*, "dimnames")=List of 2
>  ..$ : chr [1:50] "2004-01-07 15:50:00" "2004-01-07 15:55:00" "2004-01-07
> 16:00:00" "2004-01-07 16:05:00" ...
>  ..$ : chr [1:6] "Datetime" "Open" "High" "Low" ...
>  Indexed by:  POSIXct[1:50], format: "2004-01-07 15:50:00" "2004-01-07
> 15:55:00" "2004-01-07 16:00:00" "2004-01-07 16:05:00" ...
>  xts Attributes:
>  NULL
>
> dji2[1:4,]
>                    Datetime            Open  High  Low   Close Volume
> 2004-01-07 15:50:00 2004-01-07 15:50:00 10483 10483 10480 10480   0
> 2004-01-07 15:55:00 2004-01-07 15:55:00 10479 10479 10465 10465   0
> 2004-01-07 16:00:00 2004-01-07 16:00:00 10464 10464 10460 10460   0
> 2004-01-07 16:05:00 2004-01-07 16:05:00 10459 10459 10442 10442   0
>
> If I try to use barChart function (quantmod package), I will get this error:
> Error in checkSlotAssignment(object, name, value) :
>  assignment of an object of class "character" is not valid for slot
> "yrange" in an object of class "chob"; is(value, "numeric") is not TRUE
>
> The problem lies in the type of columns - open, high, low and close have
> type character, but quantmod function expects integer:
>
> if (is.OHLC(x)) {
>        chob at yrange <- c(min(Lo(x), na.rm = TRUE), max(Hi(x),
>            na.rm = TRUE))
>    }
>    else chob at yrange <- range(x[, 1], na.rm = TRUE)
>
> is.character(dji2[,5])
> [1] TRUE
>
> Any idea how to fix it?
> Thank you.
> --
> View this message in context: http://www.nabble.com/xts-and-barChart-%28quantmod%29-tp18617785p18617785.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.



-- 
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



More information about the R-SIG-Finance mailing list