跳到內容

base::match()base::%in% 不是泛型函數,因此我們無法直接為它們定義 Arrow 方法。這些函數公開了 Arrow C++ 函式庫中類似的功能。

用法

match_arrow(x, table, ...)

is_in(x, table, ...)

參數

x

ScalarArrayChunkedArray

table

ScalarArrayChunkedArray 或 R 向量查詢表。

...

額外參數,已忽略

match_arrow() 傳回一個與 x 相同長度和類型的 int32 類型 Arrow 物件,其中包含 (從 0 開始的) table 索引。is_in() 傳回一個與 x 相同長度和類型的 boolean 類型 Arrow 物件,其值指示 x 的每個元素是否存在於 table 中。

範例

# note that the returned value is 0-indexed
cars_tbl <- arrow_table(name = rownames(mtcars), mtcars)
match_arrow(Scalar$create("Mazda RX4 Wag"), cars_tbl$name)
#> Scalar
#> 1

is_in(Array$create("Mazda RX4 Wag"), cars_tbl$name)
#> Array
#> <bool>
#> [
#>   true
#> ]

# Although there are multiple matches, you are returned the index of the first
# match, as with the base R equivalent
match(4, mtcars$cyl) # 1-indexed
#> [1] 3
match_arrow(Scalar$create(4), cars_tbl$cyl) # 0-indexed
#> Scalar
#> 2

# If `x` contains multiple values, you are returned the indices of the first
# match for each value.
match(c(4, 6, 8), mtcars$cyl)
#> [1] 3 1 5
match_arrow(Array$create(c(4, 6, 8)), cars_tbl$cyl)
#> Array
#> <int32>
#> [
#>   2,
#>   0,
#>   4
#> ]

# Return type matches type of `x`
is_in(c(4, 6, 8), mtcars$cyl) # returns vector
#> Array
#> <bool>
#> [
#>   true,
#>   true,
#>   true
#> ]
is_in(Scalar$create(4), mtcars$cyl) # returns Scalar
#> Scalar
#> true
is_in(Array$create(c(4, 6, 8)), cars_tbl$cyl) # returns Array
#> Array
#> <bool>
#> [
#>   true,
#>   true,
#>   true
#> ]
is_in(ChunkedArray$create(c(4, 6), 8), cars_tbl$cyl) # returns ChunkedArray
#> ChunkedArray
#> <bool>
#> [
#>   [
#>     true,
#>     true,
#>     true
#>   ]
#> ]