Skip to content

[Feature request] Support negative index in tibble::add_row() and tibble::add_column() #1683

Description

@zhuxr11

In <tidy-select> style, negative index is supported. For example: dplyr::relocate(df, col, .after = -1) moves col column to the last. However, this does not hold true for tibble::add_row() or tibble::add_column(). In these two functions, use .after = -1 will insert at the FIRST row/column. Can we take tidy-select style to facilitate inserting at the last n row/column by specifying after = -n or before = -n?

tibble::tibble(x = 1:5) %>% tibble::add_row(x = 7, .after = -1)
# A tibble: 6 × 1
      x
  <dbl>
1     7
2     1
3     2
4     3
5     4
6     5

tibble::tibble(x = 1:5) %>% tibble::add_column(y = 7, .after = -1)
# A tibble: 5 × 2
      y     x
  <dbl> <int>
1     7     1
2     7     2
3     7     3
4     7     4
5     7     5

In <tidy-select> style like dplyr::relocate():

tibble::tibble(x = 1:5) %>% tibble::add_column(y = 7, .after = -1) %>% dplyr::relocate(y, .after = -1)
# A tibble: 5 × 2
      x     y
  <int> <dbl>
1     1     7
2     2     7
3     3     7
4     4     7
5     5     7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions