跳到內容

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):建構陣列的零複製切片,具有指定的偏移量和長度。如果長度為 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