Skip to content

[R] Add slice() method #29395

Open
Open

Description

@asfimport

Rescoped to be just slice(). The other functions were implemented in ARROW-13766.

Original description:


Implement slice(), slice_head(), and slice_tail() methods for ArrowTabular, Dataset, and arrow_dplyr_query objects . I believe this should be relatively straightforward, using Take() to return only the specified rows. We already have a head() method which I believe we can reuse for slice_head().

Reporter: Ian Cook / @ianmcook

Related issues:

Note: This issue was originally created as ARROW-13767. Please see the migration documentation for further details.

Activity

  1. asfimport commented on Aug 30, 2021

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    Just a note that there is also a C++ Table::Slice method that can already be directly used for this (not a compute kernel, though, but method on the Table). The pyarrow.Table.slice method uses that (

    arrow/python/pyarrow/table.pxi

    Lines 1307 to 1334 in 26a34c3

    def slice(self, offset=0, length=None):
    """
    Compute zero-copy slice of this Table.
    Parameters
    ----------
    offset : int, default 0
    Offset from start of table to slice.
    length : int, default None
    Length of slice (default is until end of table starting from
    offset).
    Returns
    -------
    Table
    """
    cdef shared_ptr[CTable] result
    if offset < 0:
    raise IndexError('Offset must be non-negative')
    offset = min(len(self), offset)
    if length is None:
    result = self.table.Slice(offset)
    else:
    result = self.table.Slice(offset, length)
    return pyarrow_wrap_table(result)
    ).

  2. asfimport commented on Aug 30, 2021

    @asfimport
    Author

    Neal Richardson / @nealrichardson:
    Right, we use Slice internally

  3. asfimport commented on Sep 20, 2021

    @asfimport
    Author

    Neal Richardson / @nealrichardson:
    I'm not sure about these (and about head/tail in general now) since dataset scans are no longer in deterministic order. How do dbplyr backends support these for databases that behave similarly?

  4. asfimport commented on Sep 20, 2021

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:

    since dataset scans are no longer in deterministic order

    What do you mean exactly? (did something change recently? I thought the scans can have deterministic order if needed) For example the Scanner has a Head method to returns the first n rows (I would suppose this is deterministic?)

  5. asfimport commented on Sep 20, 2021

    @asfimport
    Author

    Weston Pace / @westonpace:
    This is an interesting question. The datasets API does have an ordered scan. This is used by the head method.

    However, R has moved away from the datasets API and is using the exec plan directly now. The datasets API's ordering is not part of the exec plan.

  6. asfimport commented on Sep 20, 2021

    @asfimport
    Author

    Jonathan Keane / @jonkeane:
    The dplyr docs say they "do not work with relational databases"

    Though, IME, many real-world uses of slice(), slice_head(), slice_tail() happen after an arrange:

    library(dplyr)
    
    mtcars %>% 
      group_by(cyl) %>% 
      arrange(mpg) %>% 
      slice_head(n = 2)

    These are better written using slice_min(), though there has been some evolution around that + top_n() and the like. I've seen code like the above (with arrange + slice_head) a lot.

  7. asfimport commented on Sep 20, 2021

    @asfimport
    Author

    Weston Pace / @westonpace:
    This has come up a few times and I think the resolution is always "we'll probably have to figure that out at some point" so I went ahead and sent something to the ML so we can have the conversation.

  8. asfimport commented on Feb 28, 2022

    @asfimport
    Author

    Vitalie Spinu:
    This one turned to be a blocker for us during a conversion of a project from a SQL DB to  arrow dataset.

    How does one go with slicing within a group before this issue is fixed? 

    tbl %>% 
      group_by(simulator, model) %>%
      slice(n = 1) %>%
      collect()
  9. added
    Status: stale-warning
    and removed on Sep 23, 2026
  10. thisisnic commented on Sep 23, 2026

    @thisisnic
    Member

    This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this improvement is still desired but has no current owner, please add the 'Status: needs champion' label.

  11. removed
    Status: stale-warning
    on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions