Skip to content

[Suggestion] shifts and partitions #1598

Closed

Description

@tzakharko

I would like vctrs to add support for shift and partition operations.

vec_shift(x, amount, fill) — shifts the vector by positive or negative amount and fills up the gaps with fill. The result has the same size as the input. This is basically dplyr::lag() and dplyr::lead() and is useful for similar applications.

vec_partition(x, indices) — chops the vector into slices along the monotonously increasing sequence of indices. This is useful for inserting items at arbitrary locations in a vector. Examples:

vec_partition(x, 5) ---> list(x[..5], x[6..]) 
vec_partition(x, 0) ---> list(x[0], x)
vec_partition(x, length(x)) ---> list(x, x[0])
vec_partition(x, c(2, 5, 10)) ---> list(x[..2], x[3..5], x[6..10], x[11...])

If the maintainers are interested in this kind of functionality I can submit a PR.

Activity

  1. DavisVaughan commented on Jul 27, 2022

    @DavisVaughan
    Member

    FWIW the dplyr functions are now based on a common (not exposed) shift() helper
    https://github.com/tidyverse/dplyr/blob/17c7a5d8d208f3c89d023c4735c14a74ee336dbd/R/lead-lag.R#L89

    I do think that would be useful as a helper outside of dplyr, but im not sure that vctrs is the right place for that one. We've got a long term goal of moving higher level (user facing) vector functions like that to https://github.com/tidyverse/funs


    vec_partition() is similar to (possibly a duplicate of) #1210

  2. tzakharko commented on Jul 27, 2022

    @tzakharko
    Author

    Thanks, I missed #1210 when I was looking for similar stuff.

    Personally, I think shifts and other advanced vector operations (e.g. compact) would be a great fit for vctrs, which already offers optimised advanced SIMD-style operations such as interleave and scatter/gather (vec_chop).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions