Export the Series as an R vector
Description
Export the Series as an R vector.
Usage
<series>$_to_r_vector(
...,
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
…
|
These dots are for future extensions and must be empty. |
uint8
|
Determine how to convert Polars’ UInt8 type values to R type. One of the
followings:
|
int64
|
Determine how to convert Polars’ Int64, UInt32, or UInt64 type values to
R type. One of the followings:
|
date
|
Determine how to convert Polars’ Date type values to R class. One of the
followings:
|
time
|
Determine how to convert Polars’ Time type values to R class. One of the
followings:
|
struct
|
Determine how to convert Polars’ Struct type values to R class. One of
the followings:
|
decimal
|
Determine how to convert Polars’ Decimal type values to R type. One of
the followings:
|
as_clock_class
|
A logical value indicating whether to export datetimes and duration as
the clock package’s classes.
|
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:
|
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:
|
Details
The class/type of the exported object depends on the data type of the Series as follows:
- Boolean: logical.
-
UInt8: integer or raw, depending on the
uint8
argument. - UInt16, Int8, Int16, Int32: integer.
-
Int64, UInt32, UInt64: double, character, integer, or bit64::integer64,
depending on the
int64
argument. - Float32, Float64: double.
- Decimal: double.
- String: character.
- Categorical: factor.
-
Date: Date or data.table::IDate, depending on the
date
argument. -
Time: hms::hms or data.table::ITime, depending on the
time
argument. -
Datetime (without timezone): POSIXct or clock_naive_time, depending on
the
as_clock_class
argument. -
Datetime (with timezone): POSIXct or clock_zoned_time, depending on the
as_clock_class
argument. -
Duration: difftime or clock_duration, depending on the
as_clock_class
argument. - Binary: blob::blob.
- Null: vctrs::unspecified.
- List, Array: vctrs::list_of.
-
Struct: data.frame or tibble, depending on the
struct
argument.
Value
A vector
Examples
library("polars")
# Struct values handling
series_struct <- as_polars_series(
data.frame(
a = 1:2,
b = I(list(data.frame(c = "foo"), data.frame(c = "bar")))
)
)
series_struct
#> shape: (2,)
#> Series: '' [struct[2]]
#> [
#> {1,[{"foo"}]}
#> {2,[{"bar"}]}
#> ]
#> a b
#> 1 1 foo
#> 2 2 bar
#> # A tibble: 2 × 2
#> a b
#> <int> <list<tibble[,1]>>
#> 1 1 [1 × 1]
#> 2 2 [1 × 1]
#> shape: (3,)
#> Series: '' [u8]
#> [
#> null
#> 0
#> 255
#> ]
#> [1] NA 0 255
#> [1] 00 00 ff
# Other Integer values handlings
series_uint64 <- as_polars_series(
c(NA, "0", "4294967295", "18446744073709551615")
)$cast(pl$UInt64)
series_uint64
#> shape: (4,)
#> Series: '' [u64]
#> [
#> null
#> 0
#> 4294967295
#> 18446744073709551615
#> ]
#> [1] NA 0.000000e+00 4.294967e+09 1.844674e+19
#> [1] NA "0" "4294967295"
#> [4] "18446744073709551615"
#> [1] NA 0 NA NA
# Export UInt64 as bit64::integer64 (overflow occurs)
if (requireNamespace("bit64", quietly = TRUE)) {
series_uint64$to_r_vector(int64 = "integer64")
}
#> integer64
#> [1] <NA> 0 4294967295 <NA>
# Duration values handling
series_duration <- as_polars_series(
c(NA, -1000000000, -10, -1, 1000000000)
)$cast(pl$Duration("ns"))
series_duration
#> shape: (5,)
#> Series: '' [duration[ns]]
#> [
#> null
#> -1s
#> -10ns
#> -1ns
#> 1s
#> ]
#> Time differences in secs
#> [1] NA -1e+00 -1e-08 -1e-09 1e+00
# Export Duration as clock_duration
if (requireNamespace("clock", quietly = TRUE)) {
series_duration$to_r_vector(as_clock_class = TRUE)
}
#> <duration<nanosecond>[5]>
#> [1] NA -1000000000 -10 -1 1000000000
# Datetime values handling
series_datetime <- as_polars_series(
as.POSIXct(
c(NA, "1920-01-01 00:00:00", "1970-01-01 00:00:00", "2020-01-01 00:00:00"),
tz = "UTC"
)
)$cast(pl$Datetime("ns", "UTC"))
series_datetime
#> shape: (4,)
#> Series: '' [datetime[ns, UTC]]
#> [
#> null
#> 1920-01-01 00:00:00 UTC
#> 1970-01-01 00:00:00 UTC
#> 2020-01-01 00:00:00 UTC
#> ]
#> [1] NA "1920-01-01 UTC" "1970-01-01 UTC" "2020-01-01 UTC"
# Export zoned datetime as clock_zoned_time
if (requireNamespace("clock", quietly = TRUE)) {
series_datetime$to_r_vector(as_clock_class = TRUE)
}
#> <zoned_time<nanosecond><UTC>[4]>
#> [1] NA "1920-01-01T00:00:00.000000000+00:00"
#> [3] "1970-01-01T00:00:00.000000000+00:00" "2020-01-01T00:00:00.000000000+00:00"