1

This must be very simple, but I can't seem to find the solution. I need to compare the Close value for 2 xts objects

I have 2 xts objects:

>low2
low2           daco.Close
2013-07-24     6.63

>low3
low3           daco.Close
2013-07-24     2.63

But when I try something like this

if(low2$daco.Close < low3$daco.Close) {...}

I keep getting the error

Error in if (low2$daco.Close < low3$daco.Close) { : 
  argument is of length zero

Even though when I try print(low2) or print(low3) I get the correct values.

Any tip on how to solve this will be much appreciated. Thanks!

Edit: Following are the outputs of low2 and low3

> dput(low2)
structure(6.63, .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", src = "xxx", updated = structure(1374782893.98805, class = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), index = structure(1374451200, tzone = "UTC", tclass = "Date"), .Dim = c(1L, 
1L), .Dimnames = list(NULL, "daco.Close"))
> dput(low3)
structure(2.63, .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", src = "xxx", updated = structure(1374782893.98805, class = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), index = structure(1374624000, tzone = "UTC", tclass = "Date"), .Dim = c(1L, 
1L), .Dimnames = list(NULL, "daco.Close"))
GSee
  • 48,880
  • 13
  • 125
  • 145
JordanBelf
  • 3,208
  • 9
  • 47
  • 80
  • 1
    Those don't look like xts objects. Please provide the output of `dput(low2)` and `dput(low3)` – GSee Jul 25 '13 at 18:39
  • This is the output `structure(6.63, .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", src = "xxx", updated = structure(1374782893.98805, class = c("POSIXct", "POSIXt")), class = c("xts", "zoo"), index = structure(1374624000, tzone = "UTC", tclass = "Date"), .Dim = c(1L, 1L), .Dimnames = list(NULL, "daco.Close"))` Also maybe this helps, `low2 = low2$daco.Close` and when I call low2 I get the output I posted in the question. Thanks! – JordanBelf Jul 25 '13 at 20:11
  • Edit that into your question and also provide the output of `dput(low3)`. Based on what you've provided, all I can say is that it is not reproducible because `if (low2$daco.Close < low2$daco.Close) TRUE` does not give the error you show. – GSee Jul 25 '13 at 20:15
  • Hello, thanks for your time. Indeed if I compare `low2` against itself it works, the same happens with `low3`. Them problem appears when I compare them against each other. – JordanBelf Jul 25 '13 at 20:24
  • Thank you. Note that the objects do not have the same Date like you initially showed. That makes a difference. – GSee Jul 25 '13 at 21:25
  • As an aside, it seems a little dangerous to be using syntax like `low2$daco.Close` in an `if` statement like that. What if there's more than one row in the xts? – GSee Jul 25 '13 at 21:35

1 Answers1

1

xts objects are aligned by index before operations are done. Note that low2$daco.Close + low3$daco.Close probably doesn't return what you thought it would either.

This is what you are passing to your if statement:

> low2$daco.Close < low3$daco.Close     
     [,1]
> str(low2$daco.Close < low3$daco.Close)
An 'xts' object of zero-width

You can use coredata on one or both of the xts objects.

low2$daco.Close < coredata(low3$daco.Close)
#           daco.Close
#2013-07-22      FALSE

or you could coerce one or both of them to numeric first

as.numeric(low2$daco.Close) < as.numeric(low3$daco.Close)
#[1] FALSE

If they did in fact have the same index it should have just worked.

index(low3) <- index(low2)
low2$daco.Close < low3$daco.Close
#           daco.Close
#2013-07-22      FALSE
GSee
  • 48,880
  • 13
  • 125
  • 145