Skip to content

Apply every value with an R fun

Source code

Description

About as slow as regular non-vectorized R. Similar to using R sapply on a vector.

Usage

<Series>$map_elements(
  fun,
  datatype = NULL,
  strict_return_type = TRUE,
  allow_fail_eval = FALSE
)

Arguments

fun r function, should take a single value as input and return one.
datatype DataType of return value. Default NULL means same as input.
strict_return_type bool, default TRUE: fail on wrong return type, FALSE: convert to polars Null
allow_fail_eval bool, default FALSE: raise R fun error, TRUE: convert to polars Null

Value

Series

Examples

library("polars")

s = as_polars_series(letters[1:5], "ltrs")
f = \(x) paste(x, ":", as.integer(charToRaw(x)))
s$map_elements(f, pl$String)
#> polars Series: shape: (5,)
#> Series: 'ltrs_apply' [str]
#> [
#>  "a : 97"
#>  "b : 98"
#>  "c : 99"
#>  "d : 100"
#>  "e : 101"
#> ]
# same as
as_polars_series(sapply(s$to_r(), f), s$name)
#> polars Series: shape: (5,)
#> Series: 'ltrs' [str]
#> [
#>  "a : 97"
#>  "b : 98"
#>  "c : 99"
#>  "d : 100"
#>  "e : 101"
#> ]