Skip to content

Export the polars object as an R list

Description

These S3 methods call as_polars_df(x, …)$get_columns() with rlang::set_names(), or, as_polars_df(x, …)$to_struct()$to_r_vector() |> as.list() depending on the as_series argument.

Usage

## S3 method for class 'polars_data_frame'
as.list(
  x,
  ...,
  as_series = TRUE,
  uint8 = c("integer", "raw"),
  int64 = c("double", "character", "integer", "integer64"),
  date = c("Date", "IDate"),
  time = c("hms", "ITime"),
  struct = c("dataframe", "tibble"),
  decimal = c("double", "character"),
  as_clock_class = FALSE,
  ambiguous = c("raise", "earliest", "latest", "null"),
  non_existent = c("raise", "null")
)

# S3 method for class 'polars_lazy_frame'
as.list(
  x,
  ...,
  as_series = TRUE,
  uint8 = c("integer", "raw"),
  int64 = c("double", "character", "integer", "integer64"),
  date = c("Date", "IDate"),
  time = c("hms", "ITime"),
  struct = c("dataframe", "tibble"),
  decimal = c("double", "character"),
  as_clock_class = FALSE,
  ambiguous = c("raise", "earliest", "latest", "null"),
  non_existent = c("raise", "null")
)

Arguments

x A polars object
Passed to as_polars_df().
as_series Whether to convert each column to an R vector or a Series. If TRUE (default), return a list of Series, otherwise a list of vectors.
uint8 Determine how to convert Polars’ UInt8 type values to R type. One of the followings:
  • “integer” (default): Convert to the R’s integer type.
  • “raw”: Convert to the R’s raw type. If the value is null, export as 00.
int64 Determine how to convert Polars’ Int64, UInt32, or UInt64 type values to R type. One of the followings:
  • “double” (default): Convert to the R’s double type. Accuracy may be degraded.
  • “character”: Convert to the R’s character type.
  • “integer”: Convert to the R’s integer type. If the value is out of the range of R’s integer type, export as NA_integer\_.
  • “integer64”: Convert to the bit64::integer64 class. The bit64 package must be installed. If the value is out of the range of bit64::integer64, export as bit64::NA_integer64\_.
date Determine how to convert Polars’ Date type values to R class. One of the followings:
  • “Date” (default): Convert to the R’s Date class.
  • “IDate”: Convert to the data.table::IDate class.
time Determine how to convert Polars’ Time type values to R class. One of the followings:
  • “hms” (default): Convert to the hms::hms class. If the hms package is not installed, a warning will be shown.
  • “ITime”: Convert to the data.table::ITime class. The data.table package must be installed.
struct Determine how to convert Polars’ Struct type values to R class. One of the followings:
  • “dataframe” (default): Convert to the R’s data.frame class.
  • “tibble”: Convert to the tibble class. If the tibble package is not installed, a warning will be shown.
decimal Determine how to convert Polars’ Decimal type values to R type. One of the followings:
  • “double” (default): Convert to the R’s double type.
  • “character”: Convert to the R’s character type.
as_clock_class A logical value indicating whether to export datetimes and duration as the clock package’s classes.
  • FALSE (default): Duration values are exported as difftime and datetime values are exported as POSIXct. Accuracy may be degraded.
  • TRUE: Duration values are exported as clock_duration, datetime without timezone values are exported as clock_naive_time, and datetime with timezone values are exported as clock_zoned_time. For this case, the clock package must be installed. Accuracy will be maintained.
ambiguous Determine how to deal with ambiguous datetimes. Only applicable when as_clock_class is set to FALSE and datetime without timezone values are exported as POSIXct. Character vector or expression containing the followings:
  • “raise” (default): Throw an error
  • “earliest”: Use the earliest datetime
  • “latest”: Use the latest datetime
  • “null”: Return a NA value
non_existent Determine how to deal with non-existent datetimes. Only applicable when as_clock_class is set to FALSE and datetime without timezone values are exported as POSIXct. One of the followings:
  • “raise” (default): Throw an error
  • “null”: Return a NA value

Details

Arguments other than x and as_series are passed to \<Series>$to_r_vector(), so they are ignored when as_series=TRUE.

Value

A list

See Also

  • \$get_columns()

Examples

library("polars")

df <- as_polars_df(list(a = 1:3, b = 4:6))

as.list(df, as_series = TRUE)
#> $a
#> shape: (3,)
#> Series: 'a' [i32]
#> [
#>  1
#>  2
#>  3
#> ]
#> 
#> $b
#> shape: (3,)
#> Series: 'b' [i32]
#> [
#>  4
#>  5
#>  6
#> ]
as.list(df, as_series = FALSE)
#> $a
#> [1] 1 2 3
#> 
#> $b
#> [1] 4 5 6
as.list(df$lazy(), as_series = TRUE)
#> $a
#> shape: (3,)
#> Series: 'a' [i32]
#> [
#>  1
#>  2
#>  3
#> ]
#> 
#> $b
#> shape: (3,)
#> Series: 'b' [i32]
#> [
#>  4
#>  5
#>  6
#> ]
as.list(df$lazy(), as_series = FALSE)
#> $a
#> [1] 1 2 3
#> 
#> $b
#> [1] 4 5 6