Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subset assignment when only 'i' is provided #244

Open
joshuaulrich opened this issue May 21, 2018 · 0 comments
Open

Subset assignment when only 'i' is provided #244

joshuaulrich opened this issue May 21, 2018 · 0 comments
Labels
question User questions and open questions about development

Comments

@joshuaulrich
Copy link
Owner

There's slightly different behavior between "matrix" and xts/zoo when it comes to subset and subset-assignment calls that only specify i. For example, using the data from a stack overflow question:

dDate <- as.Date("1991-01-02")
m1 <- cbind(trade_level = 1, t2 = 3)
m2 <- cbind(trade_level = 5, t2 = 0.053)
x1 <- xts(m1, dDate)
x2 <- xts(m2, dDate)
z1 <- zoo(m1, dDate)
z2 <- zoo(m2, dDate)

Consider the behavior of each class if only i is specified when subsetting. The matrix object returns the first element, while both the xts and zoo objects return all the columns for the first row.

m1[1L]
# [1] 1
x1[1L]
#            trade_level t2
# 1991-01-02           1  3
z1[1L]
#            trade_level t2
# 1991-01-02           1  3

Now compare the 3 classes' behavior when assigning a new row to a subset when only i is provided:

m1[1L] <- m2
# Warning message:
# In m1[1L] <- m2 :
#   number of items to replace is not a multiple of replacement length
x1[1L] <- x2
# Warning message:
# In x1[1L] <- x2 :
#   number of items to replace is not a multiple of replacement length
z1[1L] <- z2
# Warning message:
# In z1[1L] <- z2 :
#   number of items to replace is not a multiple of replacement length

They all warn, and none of the subset assignment calls alter the original object. The result isn't too surprising for matrix, since the subset call only returns one element. But it's a bit surprising for xts and zoo because the subset call returns the entire row, so it seems like you should be able to replace an entire row.

@joshuaulrich joshuaulrich added the question User questions and open questions about development label May 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question User questions and open questions about development
Projects
None yet
Development

No branches or pull requests

1 participant