跳过内容

Array 是一个具有某种逻辑类型和长度的不可变数据数组。大多数逻辑类型包含在基础 Array 类中;还有 DictionaryArrayListArrayStructArray 的子类。

工厂

Array$create() 工厂方法实例化一个 Array 并接受以下参数

  • x: 一个 R 向量、列表或 data.frame

  • type: x 的可选数据类型。如果省略,将从数据中推断类型。

当给定 R 因子时,Array$create() 将返回 Array 的相应子类,例如 DictionaryArray

要直接构成 DictionaryArray,请调用 DictionaryArray$create(),它接受两个参数

  • x: 一个用于字典索引的 R 整数向量或 Array 数组

  • dict: 一个 R 向量或 Array 字典值数组(类似于 R 因子级别,但不限于字符串)

用法

a <- Array$create(x)
length(a)

print(a)
a == a

方法

  • $IsNull(i): 如果索引处的值为空,则返回 true。不进行边界检查

  • $IsValid(i): 如果索引处的值有效,则返回 true。不进行边界检查

  • $length(): 此数组包含的元素数量的大小

  • $nbytes(): 数组元素消耗的总字节数

  • $offset: 相对于另一个数组数据的相对位置,用于实现零拷贝切片

  • $null_count: 数组中空条目的数量

  • $type: 数据的逻辑类型

  • $type_id(): 类型 ID

  • $Equals(other): 此数组是否等于 other

  • $ApproxEquals(other) :

  • $Diff(other): 返回一个表示两个数组之间差异的字符串

  • $data(): 返回底层 ArrayData

  • $as_vector():转换为 R 向量

  • $ToString(): 数组的字符串表示

  • $Slice(offset, length = NULL): 使用指示的偏移量和长度构造数组的零拷贝切片。如果 length 为 NULL,则切片直到数组末尾。

  • $Take(i): 返回一个 Array,其值位于由整数(R 向量或 Array 数组)i 给定的位置。

  • $Filter(i, keep_na = TRUE): 返回一个 Array,其值位于逻辑向量(或 Arrow 布尔数组)iTRUE 的位置。

  • $SortIndices(descending = FALSE): 返回一个整数位置的 Array,可用于按升序或降序重新排列 Array

  • $RangeEquals(other, start_idx, end_idx, other_start_idx) :

  • $cast(target_type, safe = TRUE, options = cast_options(safe)): 更改数组中的数据以改变其类型。

  • $View(type): 使用给定类型构造此数组的零拷贝视图。

  • $Validate(): 执行任何验证检查以确定数组内部数据中的明显不一致。这可能是一个昂贵的检查,可能是 O(length)

示例

my_array <- Array$create(1:10)
my_array$type
#> Int32
#> int32
my_array$cast(int8())
#> Array
#> <int8>
#> [
#>   1,
#>   2,
#>   3,
#>   4,
#>   5,
#>   6,
#>   7,
#>   8,
#>   9,
#>   10
#> ]

# Check if value is null; zero-indexed
na_array <- Array$create(c(1:5, NA))
na_array$IsNull(0)
#> [1] FALSE
na_array$IsNull(5)
#> [1] TRUE
na_array$IsValid(5)
#> [1] FALSE
na_array$null_count
#> [1] 1

# zero-copy slicing; the offset of the new Array will be the same as the index passed to $Slice
new_array <- na_array$Slice(5)
new_array$offset
#> [1] 5

# Compare 2 arrays
na_array2 <- na_array
na_array2 == na_array # element-wise comparison
#> Array
#> <bool>
#> [
#>   true,
#>   true,
#>   true,
#>   true,
#>   true,
#>   null
#> ]
na_array2$Equals(na_array) # overall comparison
#> [1] TRUE