[R] Another quantmod question

Joshua Wiley jwiley.psych at gmail.com
Mon May 9 04:55:46 CEST 2011


On Sun, May 8, 2011 at 1:17 PM, Russ Abbott <russ.abbott at gmail.com> wrote:
> I understand Josh's example:
>
> mat <- matrix(1:10, dimnames = list(NULL, "A"))
> cbind(X = 11:20, Y = mat + 1)
> cbind(X = 11:20, Y = mat[, "A"] + 1)
>
> In the line, cbind(X = 11:20, Y = mat + 1), it would be nice if an error or
> warning message were issued to the effect that the "Y = " part is ignored or
> not applicable or something to that effect.
> I still don't understand why cbind.xts doesn't do what I expect.  If I  try
> what Josh suggests on my xts example, the result is still confusing.
>
>> cbind.xts(close, A = close[,"close"])
>
>              close close.1
>
> 2007-01-03 1416.60 1416.60
>
> 2007-01-04 1418.34 1418.34
>
> 2007-01-05 1409.71 1409.71
>
> 2007-01-08 1412.84 1412.84
>
> 2007-01-09 1412.11 1412.11
>
>
> If this helps:
>
>> dput(close)
>
> structure(c(1416.6, 1418.34, 1409.71, 1412.84, 1412.11), .indexCLASS =
> "Date", .indexTZ = structure("", .Names = "TZ"), src = "yahoo", updated =
> structure(1304875186.625, class = c("POSIXct",
>
> "POSIXt")), class = c("xts", "zoo"), index = structure(c(1167811200,
>
> 1167897600, 1167984000, 1168243200, 1168329600), tzone = structure("",
> .Names = "TZ"), tclass = "Date"), .Dim = c(5L,
>
> 1L), .Dimnames = list(NULL, "close"))

It does.  Thank you.  It sounds like you want to keep the xts class,
so this may not be useful to you, but if you are willing to drop it at
the point you are cbind()ing things together:

cbind(changed.close = as.matrix(close + 1)[, "close"], zero = 0, close)

should do the trick.

Cheers,

Josh

>
> I was also unable to convert Josh's matrix example to xts.
>
>> as.xts(mat, order.by = rownames())
>
> Error in dimnames(x) : 'x' is missing
>
> The order.by parameter is required, but I don't understand the error message
> and haven't been able to figure out how to fix it. If this helps:
>
>> dput(mat)
> structure(1:10, .Dim = c(10L, 1L), .Dimnames = list(NULL, "A"))
>
> This suggests that mat already has defined dimnames. So why does as.xts give
> an error message about dimnames?  I was unable to figure out the problem by
> looking at any of the help files. In particular the help file
> for as.xts.methods didn't seem to offer any clarifications--at least none
> that I could understand.
>
> -- Russ
>
>
> On Sun, May 8, 2011 at 12:26 PM, Joshua Wiley <jwiley.psych at gmail.com>
> wrote:
>>
>> class(mat)
>> class(mat[, "A"])
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list