Skip to content

Add by to chop() too #1490

Open
Open
@DavisVaughan

Description

@DavisVaughan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions