Skip to content

Create sequence of evenly-spaced points for each row between start and end

Source code

Description

[Experimental] The number of values in each sequence is determined by num_samples.

Usage

pl$linear_spaces(
  start,
  end,
  num_samples,
  ...,
  closed = c("both", "left", "none", "right"),
  as_array = FALSE
)

Arguments

start Lower bound of the range.
end Upper bound of the range.
num_samples Number of samples in the output sequence.
… These dots are for future extensions and must be empty.
closed Define which sides of the range are closed (inclusive). One of the following: “both” (default), “left”, “right”, “none”.
as_array Return result as a fixed-length Array. num_samples must be a constant.

Details

linear_space works with numeric and temporal dtypes. When the start and end parameters are Date dtypes, the output sequence consists of equally-spaced Datetime elements with millisecond precision.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(start = c(1, -1), end = c(3, 2), num_samples = c(4, 5))
df$with_columns(ls = pl$linear_spaces("start", "end", "num_samples"))
#> shape: (2, 4)
#> ┌───────┬─────┬─────────────┬────────────────────────┐
#> │ start ┆ end ┆ num_samples ┆ ls                     │
#> │ ---   ┆ --- ┆ ---         ┆ ---                    │
#> │ f64   ┆ f64 ┆ f64         ┆ list[f64]              │
#> ╞═══════╪═════╪═════════════╪════════════════════════╡
#> │ 1.0   ┆ 3.0 ┆ 4.0         ┆ [1.0, 1.666667, … 3.0] │
#> │ -1.0  ┆ 2.0 ┆ 5.0         ┆ [-1.0, -0.25, … 2.0]   │
#> └───────┴─────┴─────────────┴────────────────────────┘
df$with_columns(ls = pl$linear_spaces("start", "end", 3, as_array = TRUE))
#> shape: (2, 4)
#> ┌───────┬─────┬─────────────┬──────────────────┐
#> │ start ┆ end ┆ num_samples ┆ ls               │
#> │ ---   ┆ --- ┆ ---         ┆ ---              │
#> │ f64   ┆ f64 ┆ f64         ┆ array[f64, 3]    │
#> ╞═══════╪═════╪═════════════╪══════════════════╡
#> │ 1.0   ┆ 3.0 ┆ 4.0         ┆ [1.0, 2.0, 3.0]  │
#> │ -1.0  ┆ 2.0 ┆ 5.0         ┆ [-1.0, 0.5, 2.0] │
#> └───────┴─────┴─────────────┴──────────────────┘