Skip to content

Return the first or the last n parts of an object

Description

They are equivalent to $head() and $tail() methods.

Usage

## S3 method for class 'RPolarsDataFrame'
head(x, n = 6L, ...)

# S3 method for class 'RPolarsLazyFrame'
head(x, n = 6L, ...)

# S3 method for class 'RPolarsDataFrame'
tail(x, n = 6L, ...)

# S3 method for class 'RPolarsLazyFrame'
tail(x, n = 6L, ...)

Arguments

x A polars object
n An integer vector of length 1. Note that negative values are not supported for if x is a LazyFrame.
Ignored

Value

A polars object of the same class as x

See Also

  • \$head()
  • \$head()
  • \$tail()
  • \$tail()
  • \$fetch()

Examples

library("polars")

df = pl$DataFrame(foo = 1:5, bar = 6:10, ham = letters[1:5])
lf = df$lazy()

head(df, 2)
#> shape: (2, 3)
#> ┌─────┬─────┬─────┐
#> │ foo ┆ bar ┆ ham │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ str │
#> ╞═════╪═════╪═════╡
#> │ 1   ┆ 6   ┆ a   │
#> │ 2   ┆ 7   ┆ b   │
#> └─────┴─────┴─────┘
tail(df, 2)
#> shape: (2, 3)
#> ┌─────┬─────┬─────┐
#> │ foo ┆ bar ┆ ham │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ str │
#> ╞═════╪═════╪═════╡
#> │ 4   ┆ 9   ┆ d   │
#> │ 5   ┆ 10  ┆ e   │
#> └─────┴─────┴─────┘
head(lf, 2)
#> polars LazyFrame
#>  $explain(): Show the optimized query plan.
#> 
#> Naive plan:
#> SLICE[offset: 0, len: 2]
#>   DF ["foo", "bar", "ham"]; PROJECT */3 COLUMNS; SELECTION: None
tail(lf, 2)
#> polars LazyFrame
#>  $explain(): Show the optimized query plan.
#> 
#> Naive plan:
#> SLICE[offset: -2, len: 2]
#>   DF ["foo", "bar", "ham"]; PROJECT */3 COLUMNS; SELECTION: None
head(df, -2)
#> shape: (3, 3)
#> ┌─────┬─────┬─────┐
#> │ foo ┆ bar ┆ ham │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ str │
#> ╞═════╪═════╪═════╡
#> │ 1   ┆ 6   ┆ a   │
#> │ 2   ┆ 7   ┆ b   │
#> │ 3   ┆ 8   ┆ c   │
#> └─────┴─────┴─────┘
tail(df, -2)
#> shape: (3, 3)
#> ┌─────┬─────┬─────┐
#> │ foo ┆ bar ┆ ham │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ str │
#> ╞═════╪═════╪═════╡
#> │ 3   ┆ 8   ┆ c   │
#> │ 4   ┆ 9   ┆ d   │
#> │ 5   ┆ 10  ┆ e   │
#> └─────┴─────┴─────┘