Skip to content

uncount() should error for non-integer weights #1069

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

Closed
mgirlich opened this issue Dec 7, 2020 · 1 comment · Fixed by #1070
Closed

uncount() should error for non-integer weights #1069

mgirlich opened this issue Dec 7, 2020 · 1 comment · Fixed by #1070
Labels
feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@mgirlich
Copy link
Contributor

mgirlich commented Dec 7, 2020

uncount() currently works with non-integer weights where it probably should not. Also the error message for negative weights is quite unclear.

library(tidyr)

# no error
tibble(x = c("a", "b"), n = c(1, 0.5)) %>% 
  uncount(n)
#> # A tibble: 1 x 1
#>   x    
#>   <chr>
#> 1 a

# unclear error message
tibble(x = c("a", "b"), n = c(1, -1)) %>% 
  uncount(n)
#> Error in rep(seq_nrow(data), w): invalid 'times' argument

Created on 2020-12-07 by the reprex package (v0.3.0)

@hadley
Copy link
Member

hadley commented Feb 17, 2021

I think it's traditional for R to round down counts, e.g. rep(1:2, c(1, 0.5)), but I think you're right that the tidyverse can do better here (barring FP challenges).

@hadley hadley added feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes" labels Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants