跳转到内容

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

工厂方法

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

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

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

Array$create() 将返回 Array 的适当子类,例如当给定一个 R 因子时,将返回 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 Array)i 给定的位置处的值。

  • $Filter(i, keep_na = TRUE):返回一个 Array,其值为逻辑向量(或 Arrow 布尔值 Array)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():执行任何验证检查,以确定数组内部数据中明显的 inconsistencies。 这可能是一项昂贵的检查,可能是 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