這些函數是 map()
的變體,可同時迭代多個參數。它們是並行的,因為每個輸入都與其他輸入並行處理,而不是多核計算的意義上,即它們與 base::pmax()
和 base::pmin()
共享相同的 "parallel" 概念。
用法
pmap(.l, .f, ..., .progress = FALSE)
pmap_lgl(.l, .f, ..., .progress = FALSE)
pmap_int(.l, .f, ..., .progress = FALSE)
pmap_dbl(.l, .f, ..., .progress = FALSE)
pmap_chr(.l, .f, ..., .progress = FALSE)
pmap_vec(.l, .f, ..., .ptype = NULL, .progress = FALSE)
pwalk(.l, .f, ..., .progress = FALSE)
參數
- .l
-
向量列表。
.l
的長度決定了調用.f
時使用的參數數量。如果未命名,參數將按位置提供;如果命名,則按名稱提供。長度為1的向量將被回收到任意長度;所有其他元素必須具有相同的長度。
數據幀是
.l
的一個重要特例。它將導致每行調用一次.f
。 - .f
-
一個函數,通過以下方式之一指定:
-
一個命名函數。
-
匿名函數,例如
\(x, y, z) x + y / z
或function(x, y, z) x + y / z
-
一個公式,例如
~ ..1 + ..2 / ..3
。不建議使用此語法,因為您隻能按位置引用參數。
-
- ...
-
傳遞給映射函數的附加參數。
我們現在通常建議不要使用
...
將附加(常量)參數傳遞給.f
。相反,使用簡寫匿名函數:# Instead of x |> map(f, 1, 2, collapse = ",") # do: x |> map(\(x) f(x, 1, 2, collapse = ","))
這使得更容易理解哪些參數屬於哪個函數,並且往往會產生更好的錯誤消息。
- .progress
-
是否顯示進度條。使用
TRUE
打開基本進度條,使用字符串為其命名,或參閱progress_bars 了解更多詳細信息。 - .ptype
-
如果是
NULL
,默認情況下,輸出類型是結果元素的公共類型。否則,提供 "prototype" 給出所需的輸出類型。
值
輸出長度由輸入的長度決定。輸出名稱由輸入名稱確定。輸出類型由後綴確定:
-
無後綴:列表;
.f()
可以返回任何內容。 -
_lgl()
、_int()
、_dbl()
、_chr()
分別返回邏輯向量、整數向量、雙精度向量或字符向量;.f()
必須返回長度為 1 的兼容原子向量。 -
_vec()
返回原子或 S3 向量,與.f
返回的類型相同。.f
可以返回幾乎任何類型的向量,隻要其長度為 1。 -
walk()
返回輸入.x
(不可見)。這使得它很容易在管道中使用。.f()
的返回值被忽略。
.f
引發的任何錯誤都將包含在類 purrr_error_indexed 的錯誤中。
例子
x <- list(1, 1, 1)
y <- list(10, 20, 30)
z <- list(100, 200, 300)
pmap(list(x, y, z), sum)
#> [[1]]
#> [1] 111
#>
#> [[2]]
#> [1] 221
#>
#> [[3]]
#> [1] 331
#>
# Matching arguments by position
pmap(list(x, y, z), function(first, second, third) (first + third) * second)
#> [[1]]
#> [1] 1010
#>
#> [[2]]
#> [1] 4020
#>
#> [[3]]
#> [1] 9030
#>
# Matching arguments by name
l <- list(a = x, b = y, c = z)
pmap(l, function(c, b, a) (a + c) * b)
#> [[1]]
#> [1] 1010
#>
#> [[2]]
#> [1] 4020
#>
#> [[3]]
#> [1] 9030
#>
# Vectorizing a function over multiple arguments
df <- data.frame(
x = c("apple", "banana", "cherry"),
pattern = c("p", "n", "h"),
replacement = c("P", "N", "H"),
stringsAsFactors = FALSE
)
pmap(df, gsub)
#> [[1]]
#> [1] "aPPle"
#>
#> [[2]]
#> [1] "baNaNa"
#>
#> [[3]]
#> [1] "cHerry"
#>
pmap_chr(df, gsub)
#> [1] "aPPle" "baNaNa" "cHerry"
# Use `...` to absorb unused components of input list .l
df <- data.frame(
x = 1:3,
y = 10:12,
z = letters[1:3]
)
plus <- function(x, y) x + y
if (FALSE) {
# this won't work
pmap(df, plus)
}
# but this will
plus2 <- function(x, y, ...) x + y
pmap_dbl(df, plus2)
#> [1] 11 13 15
# The "p" for "parallel" in pmap() is the same as in base::pmin()
# and base::pmax()
df <- data.frame(
x = c(1, 2, 5),
y = c(5, 4, 8)
)
# all produce the same result
pmin(df$x, df$y)
#> [1] 1 2 5
map2_dbl(df$x, df$y, min)
#> [1] 1 2 5
pmap_dbl(df, min)
#> [1] 1 2 5
相關用法
- R purrr pluck 安全地獲取或設置嵌套數據結構深處的元素
- R purrr possibly 包裝函數以返回值而不是錯誤
- R purrr pluck_depth 計算向量的深度
- R purrr partial 部分應用函數,填充一些參數
- R purrr accumulate 累積向量縮減的中間結果
- R purrr imap 將函數應用於向量的每個元素及其索引
- R purrr list_transpose 轉置列表
- R purrr as_vector 將列表強製轉換為向量
- R purrr map_if 有條件地將函數應用於向量的每個元素
- R purrr map2 映射兩個輸入
- R purrr array-coercion 強製數組列出
- R purrr auto_browse 包裝一個函數,以便在出錯時自動 browser()
- R purrr insistently 將函數轉換為等待,然後在錯誤後重試
- R purrr map_depth 在給定深度映射/修改元素
- R purrr list_simplify 將列表簡化為原子或 S3 向量
- R purrr rerun 多次重新運行表達式
- R purrr quietly 包裝一個函數來捕獲副作用
- R purrr list_flatten 壓平列表
- R purrr head_while 查找全部滿足謂詞的頭/尾。
- R purrr rbernoulli 從伯努利分布生成隨機樣本
- R purrr rate-helpers 創建延遲率設置
- R purrr keep_at 根據元素的名稱/位置保留/丟棄元素
- R purrr keep 根據元素的值保留/丟棄元素
- R purrr transpose 轉置列表。
- R purrr flatten 將列表的列表展平為簡單的向量
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Map over multiple input simultaneously (in "parallel")。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。