Open joshuaulrich opened 7 years ago
I think that zero-row/length objects are useful when you want to model available observations in a given time frame and no observations are available for some securities. Apart from this, one could rule out them, like a division-by-zero error, but since they are currently legal objects, merge.xts
should support them. Also because, given a zero-length x
, it seems quite natural to set to NA x
column(s) in merge(x,y, all=TRUE)
and to output another zero-length xts, from merge(x,y, all=FALSE)
Another function that should be fixed for zero objects is names()
:
e <- xts(data.frame(p=numeric()), as.Date(character()))
names(e)
# NULL
The result should be "p".
Also, off topic a bit, but speaking of names:
(x <- xts(data.frame(p=1), as.Date(1)))
# p
# 1970-01-02 1
(y <- xts(c(p=1), as.Date(1)))
# [,1]
# 1970-01-02 1
could/should both name the column p
.
Thanks for the feedback, @steven001.
There is no names.xts()
method, so the names.zoo()
method is being called. And it looks like xts behaves the same as zoo in this case.
require(xts)
x1 <- .xts(data.frame(a=1, b=1), 1)
z1 <- as.zoo(x1)
x0 <- .xts(data.frame(a=numeric(), b=numeric()), numeric())
z0 <- as.zoo(x0)
names(x1) # [1] "a" "b"
names(z1) # [1] "a" "b"
names(x0) # NULL
names(z0) # NULL
In your second example, c(p=1)
is a named vector; and vectors are column-wise in R, so the names would be row names. That means your y
object should not have column names.
It's quite common to create a zero-width xts object for aligning series or making irregular series regular. A zero-width xts object has an index, but no data.
But you can also create a "zero-length" xts object by subsetting outside the bounds of the index. For example:
Note that
str(x["1900"])
returns "An 'xts' object of zero-width", which isn't true. As you can see fromdim(x["1900"])
, the "width" is 1 but there are no rows. So I would callx["1900"]
a "zero-length xts object". It's not clear to me if/how these should be supported by other functions (e.g.merge
), but at minimumstr
shouldn't say they're zero-width xts objects.