Skip to content

Commit

Permalink
Another attempt to fix rownames
Browse files Browse the repository at this point in the history
Setting .ROWNAMES caused some fts tests to fail because reclass()
called rownames<- on the xts object before calling re.fts(). And xts
objects can't have rownames, so the call was ignored and the reclassed
fts object didn't have rownames like the match.to object).

For reasons I don't understand, this call did not work.
    dimnames(xx)[[1]] <- NULL
It looks like it may be a bug in R...?

After those two changes, calls to Ops.xts() for '&' failed when it was
called on an xts object and a matrix with row names. This was because
.xts() was called on the result of the operation ('e') and 'e' had
rownames because the matrix did. Now we drop dimnames before
potentially calling .xts().

Fixes #298 (maybe)
  • Loading branch information
joshuaulrich committed Aug 8, 2020
1 parent 3e13355 commit 9550e28
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 8 additions & 7 deletions R/Ops.xts.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ function(e1, e2)
idx[] <- idx[0]
attr(e,'index') <- idx
}
dn <- dimnames(e)
if(!is.null(dn[[1L]])) {
if(is.null(dn[[2L]])) {
attr(e, "dimnames") <- NULL
} else {
dimnames(e) <- list(NULL, dn[[2L]])
}
}
if(is.null(attr(e,'index'))) {
if(is.xts(e1)) {
e <- .xts(e, .index(e1), tclass(e1), tzone(e1), tformat = tformat(e1))
Expand All @@ -74,13 +82,6 @@ function(e1, e2)
if(is.null(dim(e1)) && is.null(dim(e2)))
dim(e) <- NULL
}
if(!is.null(dimnames(e)[[1L]])) {
if(is.null(dimnames(e)[[2L]])) {
attr(e, "dimnames") <- NULL
} else {
dimnames(e)[[1]] <- list(NULL)
}
}
attr(e, "names") <- NULL
e
}
8 changes: 5 additions & 3 deletions R/xts.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function(x=NULL, index, tclass=c("POSIXct","POSIXt"),
rn <- dimnames(xx)[[1]]
if(!is.null(rn)) {
attr(xx, '.ROWNAMES') <- rn
dimnames(xx)[[1]] <- NULL
dimnames(xx)[1] <- list(NULL)
}

# remove any index attributes that came through '...'
Expand Down Expand Up @@ -232,8 +232,10 @@ function(x, match.to, error=FALSE, ...) {
if(length(oldCLASS) > 0 && !inherits(oldClass,'xts')) {
if(!is.null(dim(x))) {
if(!is.null(attr(x,'.ROWNAMES'))) {
rownames(x) <- attr(x,'.ROWNAMES')[1:NROW(x)]
} #else rownames(x) <- NULL
# rownames<- (i.e. dimnames<-.xts) will not set row names
# force them directly
attr(x, "dimnames")[[1]] <- attr(x,'.ROWNAMES')[1:NROW(x)]
}
}
attr(x,'.ROWNAMES') <- NULL
#if(is.null(attr(x,'.RECLASS')) || attr(x,'.RECLASS')) {#should it be reclassed?
Expand Down

0 comments on commit 9550e28

Please sign in to comment.