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

first() on a numeric vector with negative argument returns unexpected result #325

Closed
vxg20 opened this issue Feb 9, 2020 · 3 comments
Closed
Assignees
Labels
Milestone

Comments

@vxg20
Copy link

vxg20 commented Feb 9, 2020

Description

Hi there, there seems to be changes in the way the function first treats vector when given a negative argument.

Expected behavior

previously the function first(vect, -1) will return the original vect minus the number at the first index for xts version 0.11 but now in xts 0.12 the behaviour changes. I am not sure what it is trying to do anymore

Minimal, reproducible example

x <- c(1,2,3,4)

first(x,-1)

this will now return c(3, 4) in xts version 0.12
seems like a bug as the result is quite unexpected ?

@braverock
Copy link
Contributor

braverock commented Feb 9, 2020

The issue, such as there is one, appears to only be on vectors, and not on actual xts objects:

x<-c(1:4)

y<-xts(x,order.by=as.Date(x))

first(x)
# [1] 1

first(x,-1)
# [1] 3 4 # not as expected

y
#            [,1]
# 1970-01-02    1
# 1970-01-03    2
# 1970-01-04    3
# 1970-01-05    4

first(y,-1)
#            [,1]
# 1970-01-03    2
# 1970-01-04    3
# 1970-01-05    4 

first(y,'-1 day')
#            [,1]
# 1970-01-03    2
# 1970-01-04    3
# 1970-01-05    4

@joshuaulrich
Copy link
Owner

joshuaulrich commented Feb 9, 2020

This does look like a bug, but it's not a change in behavior in 0.12-0. This behavior was covered by a unit test before the 0.11-0 release. It was added in commit eb7cc4a. If this is a bug, it likely affects last() also.

@joshuaulrich
Copy link
Owner

joshuaulrich commented Aug 2, 2020

I just checked, and found that the existing tests use an input vector of 3 elements. They fail if they are lengthened to 4 elements. So this is certainly a bug.

@joshuaulrich joshuaulrich self-assigned this Aug 2, 2020
@joshuaulrich joshuaulrich added this to the 0.12-1 milestone Aug 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants