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

Add by to chop() too #1490

Open
DavisVaughan opened this issue Mar 6, 2023 · 0 comments
Open

Add by to chop() too #1490

DavisVaughan opened this issue Mar 6, 2023 · 0 comments
Labels
feature a feature request or enhancement

Comments

@DavisVaughan
Copy link
Member

I was thinking of this when implementing #1461 but wasn't sure if it would be useful.

But now I think it can be useful because whenever I call chop() I often think of what i want to "chop by" when i say it out loud in my head.

library(ivs)
library(dplyr, warn.conflicts = FALSE)

df <- tibble(
  id=c(rep("a",4),rep("b",2),rep("c",3)), 
  start=c(100,250,400,600,150,610,275,600,700), 
  end=c(200,300,550,650,275,640,325,675,725)
)

df <- df %>%
  mutate(range = iv(start, end), .keep = "unused")

df
#> # A tibble: 9 × 2
#>   id         range
#>   <chr>  <iv<dbl>>
#> 1 a     [100, 200)
#> 2 a     [250, 300)
#> 3 a     [400, 550)
#> 4 a     [600, 650)
#> 5 b     [150, 275)
#> 6 b     [610, 640)
#> 7 c     [275, 325)
#> 8 c     [600, 675)
#> 9 c     [700, 725)

df %>%
  tidyr::chop(range)
#> # A tibble: 3 × 2
#>   id              range
#>   <chr> <list<iv<dbl>>>
#> 1 a                 [4]
#> 2 b                 [2]
#> 3 c                 [3]

Like in this example I think it makes the most sense to say that I want to chop "by id".

If I say I want to "chop the range", you still have to ask yourself what you want to chop it by, so you implicitly always need that "by" knowledge loaded in your head anyways, so we might as well let you specify that directly instead.

Notes:

  • Signature change to chop(data, cols = NULL, ..., by = NULL), so cols is now technically optional
  • If one of cols or by is supplied, the other is inferred as "everything else"
  • We have decided in nest() that nest(data) with no other args means "nest everything" so we probably want to either be consistent with that here (i.e. "chop everything") or say that you must specify at least one of cols or by, which does feel safer.
  • Allow for both cols and by to be specified together, like nest()
  • Can probably borrow a lot from nest_info()? Probably worth trying to harmonize that between the two functions
@DavisVaughan DavisVaughan changed the title Add .by to chop() too Add by to chop() too Mar 6, 2023
@DavisVaughan DavisVaughan added the feature a feature request or enhancement label Mar 6, 2023
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
Projects
None yet
Development

No branches or pull requests

1 participant