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

Using slice() with a row number vector which includes 0 results in wrong output #3313

Closed
huftis opened this issue Jan 19, 2018 · 3 comments
Closed
Assignees
Labels
bug an unexpected problem or unintended behavior

Comments

@huftis
Copy link

huftis commented Jan 19, 2018

If one uses slice() with a row number vector which includes 0 and other row numbers, wrong output is produced. Here’s a repexp:

library(dplyr)

# Example data
d = tibble(x = 1:5, y = LETTERS[1:5])

# This command works fine (returns a zero-row subset)
slice(d, 0)
#> # A tibble: 0 x 2
#> # ... with 2 variables: x <int>, y <chr>

# But these commands give incorrect output
slice(d, c(0, 1))
#> # A tibble: 1 x 2
#>       x y    
#>   <int> <chr>
#> 1    NA <NA>
slice(d, c(0, 1, 2))
#> # A tibble: 2 x 2
#>       x y    
#>   <int> <chr>
#> 1    NA <NA> 
#> 2     1 A
slice(d, c(-1, 0))
#> # A tibble: 3 x 2
#>       x y    
#>   <int> <chr>
#> 1     3 C    
#> 2     4 D    
#> 3     5 E
slice(d, c(0, -1))
#> # A tibble: 3 x 2
#>       x y    
#>   <int> <chr>
#> 1     3 C    
#> 2     4 D    
#> 3     5 E

# Subsetting using base R functions works fine, i.e. gives the expected output
d[0, ]
#> # A tibble: 0 x 2
#> # ... with 2 variables: x <int>, y <chr>
d[c(0, 1), ]
#> # A tibble: 1 x 2
#>       x y    
#>   <int> <chr>
#> 1     1 A
d[c(0, 1, 2), ]
#> # A tibble: 2 x 2
#>       x y    
#>   <int> <chr>
#> 1     1 A    
#> 2     2 B
d[c(-1, 0), ]
#> # A tibble: 4 x 2
#>       x y    
#>   <int> <chr>
#> 1     2 B    
#> 2     3 C    
#> 3     4 D    
#> 4     5 E
d[c(0, -1), ]
#> # A tibble: 4 x 2
#>       x y    
#>   <int> <chr>
#> 1     2 B    
#> 2     3 C    
#> 3     4 D    
#> 4     5 E
@krlmlr krlmlr added bug an unexpected problem or unintended behavior data frame labels Jan 19, 2018
@krlmlr
Copy link
Member

krlmlr commented Jan 19, 2018

Thanks for reporting!

@romainfrancois romainfrancois self-assigned this Mar 12, 2018
@romainfrancois
Copy link
Member

@krlmlr taking this one too.

romainfrancois added a commit that referenced this issue Mar 12, 2018
…e refuse S4 objects.

I'm fine with that for now, and this will have to be handled in `vctrs` somehow
krlmlr added a commit that referenced this issue Mar 12, 2018
- `slice()` handles 0 in positive and negative indices (#3313).
@krlmlr krlmlr closed this as completed Mar 12, 2018
krlmlr added a commit that referenced this issue Mar 13, 2018
* `slice()` no longer enforce tibble classes when input is a simple `data.frame`, and ignores 0 (#3297, #3313).
romainfrancois added a commit that referenced this issue Mar 19, 2018
…d arrange refuse S4 objects."

This reverts commit e7a3f8a.
romainfrancois added a commit that referenced this issue Apr 9, 2018
…e refuse S4 objects.

I'm fine with that for now, and this will have to be handled in `vctrs` somehow
romainfrancois added a commit that referenced this issue Apr 9, 2018
…d arrange refuse S4 objects."

This reverts commit e7a3f8a.
@lock
Copy link

lock bot commented Sep 8, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants