- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 *apply
进行函数式编程或 purrr
是我的面包和黄油但我不明白如何pmap
处理函数参数。
# dummy data -----------------------------------------------------------------
(iter_tibble <- tibble::tibble(a = 1:2,
b = 3:4,
c = 7:6))
#> # A tibble: 2 x 3
#> a b c
#> <int> <int> <int>
#> 1 1 3 7
#> 2 2 4 6
# pmap it --------------------------------------------------------------------
# standard way
purrr::pmap(iter_tibble, function(a, b, c) {
paste(a, b, c)
})
#> [[1]]
#> [1] "1 3 7"
#>
#> [[2]]
#> [1] "2 4 6"
# switch order
# works and a maps to a, b to b etc
purrr::pmap(iter_tibble, function(b, c, a) {
paste(a, b, c)
})
#> [[1]]
#> [1] "1 3 7"
#>
#> [[2]]
#> [1] "2 4 6"
# name arguments
purrr::pmap(iter_tibble, function(a1 = a, b1 = b, c1 = c) {
paste(a1, b1, c1)
})
#> [[1]]
#> [1] "1 3 7"
#>
#> [[2]]
#> [1] "2 4 6"
# name arguments and switch order
purrr::pmap(iter_tibble, function(b1 = b, c1 = c, asterix = a) {
paste(b1, asterix, c1)
})
#> [[1]]
#> [1] "3 1 7"
#>
#> [[2]]
#> [1] "4 2 6"
# but when using a different initial letter
# ERROR
purrr::pmap(iter_tibble,
purrr::safely(
function(b1 = b, c1 = c, obelix = a) {
paste(b1, obelix, c1)
}
))[1]
#> [[1]]
#> [[1]]$result
#> NULL
#>
#> [[1]]$error
#> <simpleError in .f(...): unused argument (a = 1)>
这种行为与在常规 R 函数中调用参数的方式完全不同,在常规 R 函数中,可以使用缩写(但不好的做法)但不能进行扩展。
# regular function usage -----------------------------------------------------
# abbrevate arguments - no problem
sample(1:4, s = 5, repla = TRUE)
#> [1] 1 3 4 3 1
# extend arguments? nope
sample(1:4, size = 5, replaceeeee = TRUE)
#> Error in sample(1:4, size = 5, replaceeeee = TRUE): unused argument (replaceeeee = TRUE)
我的猜测是答案是关于
pmap
调用 C 而不是 R 中发生的事情。
最佳答案
这里的误解是您的第 3 和第 4 个选项没有“命名参数”而是默认参数值。您正在向 .f
提供函数定义pmap
的论点,而不是函数调用。pmap
正在以与基础 R 相同的方式进行部分参数匹配。打开 options(warnPartialMatchArgs = TRUE)
可能会更清楚.在这里,我将采用您的第三个示例,将函数定义分解以使正在发生的事情更清楚:
iter_tibble <- tibble::tibble(
a = 1:2,
b = 3:4,
c = 7:6
)
f3 <- function(a1 = a, b1 = b, c1 = c) {
paste(a1, b1, c1)
}
purrr::pmap(iter_tibble, f3)
#> Warning in .f(a = .l[[1L]][[i]], b = .l[[2L]][[i]], c = .l[[3L]][[i]], ...):
#> partial argument match of 'a' to 'a1'
#> Warning in .f(a = .l[[1L]][[i]], b = .l[[2L]][[i]], c = .l[[3L]][[i]], ...):
#> partial argument match of 'b' to 'b1'
#> Warning in .f(a = .l[[1L]][[i]], b = .l[[2L]][[i]], c = .l[[3L]][[i]], ...):
#> partial argument match of 'c' to 'c1'
#> Warning in .f(a = .l[[1L]][[i]], b = .l[[2L]][[i]], c = .l[[3L]][[i]], ...):
#> partial argument match of 'a' to 'a1'
#> Warning in .f(a = .l[[1L]][[i]], b = .l[[2L]][[i]], c = .l[[3L]][[i]], ...):
#> partial argument match of 'b' to 'b1'
#> Warning in .f(a = .l[[1L]][[i]], b = .l[[2L]][[i]], c = .l[[3L]][[i]], ...):
#> partial argument match of 'c' to 'c1'
#> [[1]]
#> [1] "1 3 7"
#>
#> [[2]]
#> [1] "2 4 6"
这与您描述的常规 R 函数的情况完全相同,其中提供的命名参数可以是函数参数的缩写。换句话说,对于表格的第一行,
pmap
基本构造调用
f3(a = 1, b = 3, c = 7)
.
a
,
b
, 和
c
来自列名,值来自行。
f3
没有参数
a
, 但它有一个参数
a1
.所以命名参数
a = 1
在通话中与
a1
部分匹配在函数定义中。这就是输出中描述的部分匹配警告。没有“扩展”发生。事实上,论点
a1
默认值为
a
在这里完全无关紧要。
~
匿名函数语法。使用你的第五个例子:
iter_tibble <- tibble::tibble(
a = 1:2,
b = 3:4,
c = 7:6
)
f5 <- function(b1, obelix, c1) {
paste(b1, obelix, c1)
}
f5_wrapper <- function(a, b, c) {
f5(b1 = b, obelix = a, c1 = c)
}
purrr::pmap(iter_tibble, f5_wrapper)
#> [[1]]
#> [1] "3 1 7"
#>
#> [[2]]
#> [1] "4 2 6"
purrr::pmap(iter_tibble, ~ f5(b1 = ..2, obelix = ..1, c1 = ..3))
#> [[1]]
#> [1] "3 1 7"
#>
#> [[2]]
#> [1] "4 2 6"
关于r - purrr pmap 函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66791203/
user=> (.. Runtime getRuntime availableProcessors) 2 并评估此示例:http://clojuredocs.org/clojure_core/cloj
我正在尝试确定如何使用应用于不同长度列表的 dplyr pmap 函数。 下面是我想做的一个非常简单的例子 list_1 <- list(1, 2, 3) list_2 <- list(1, 2) l
我正在尝试使用 clojure pantomime 库从大量 tif 文档(以及其他文档)中提取/ocr 文本。 我的计划是使用 pmap 将映射应用于一系列输入数据(来自 postgres 数据库)
我正在尝试将一个函数映射到包含在使用 purrr::pmap 的数据框行。我有一个 reprex下面: library(tidyverse) # Create dummy data e [[1]]
我正在尝试确定如何使用应用于不同长度列表的 dplyr pmap 函数。 下面是我想做的一个非常简单的例子 list_1 <- list(1, 2, 3) list_2 <- list(1, 2) l
我为线程编写了一个简单的 C 程序,其进程图如下所示: anirudh@anirudh-Aspire-5920:~/Documents/DUMP$ pmap 3232 3232: ./a.out
我们正在发生某种巨大的内存泄漏,我们进程的驻留内存呈指数级增长。 pmap -x 显示如下内容: ... 00007f4ad85cd000 10530276 9129608 9129608 rw---
我只是使用 pmap 对 clojure 进行一些性能测试,我希望能够控制与 pmap 一起使用的线程数。我知道在使用 OpenMP 之类的东西时,可以使用 omp_set_num_threads()
我需要在 pmap 变体的帮助下执行一些按行的操作,但是在将参数列表(即“.l”参数)传递给嵌套在另一个函数中的函数。 我尝试了各种方法,包括传递参数名称和点语法,但都无济于事。我需要知道是否有办法做
当我有一个包含两个输入列(a 和 b)的列表列数据框 (df1) 时,我可以使用 map2 获取一个新列 d1 % slice(1:10) %>% group_by(cyl) %>% nest(.ke
使用 *apply 进行函数式编程或 purrr是我的面包和黄油但我不明白如何pmap处理函数参数。 我可以用他们对应的变量名 (给定一个数据框作为输入)并且不必担心顺序。 我可以更具体地使用它们作为
我需要发出200个左右的HTTP请求。我希望它们并行运行或成批运行,而且我不确定在Clojure中从何处开始执行此操作。 pmap似乎具有我想要的效果,例如,使用http.async.client:
我试图理解为什么这段代码有效: tmp % pmap(function(this, that, other) paste(this, that, other)) ...但以下代码会产生“未使用的参数”
我想知道如何使用 pmap()函数,如果我有超过 3 个输入作为参数映射到具有其他默认输入的函数。 这是一个可重现的示例: a=c(5, 100, 900) b=c(1, 2, 3) ablist=l
我有这个数据框,我想单独使用 pmap函数仅过滤所有值为负或正的行。我正在寻找一种简洁的方法来做到这一点,因为我想到了 c(...)但我们只能在函数内部使用它。 可以通过以下代码实现: df %
我正在使用 R purrr:::pmap带三个输入。不清楚如何在公式调用中明确引用这些输入?使用 map2 时,公式调用为 ~ .x + .y .但是使用pmap的时候怎么办? 从 http://r4
我正在尝试计算一个函数,其参数是数据框中列的名称。我需要循环每一行并计算函数。看起来 pmap 是执行此操作的一种巧妙方法,但我被迫指定“..1, ..2,”符号来指示数据框中的列位置。我认为这不是一
我想在传感器中使用 pmap,如果我使用常规 map 编写代码,它工作得很好。但是,如果我使用 pmap 我会得到一个 arity 异常。是否可以在 clojure 中实现此目的,或者我做错了什么?如
我正在尝试设置一个具有多个输入和多个返回值的自定义函数,并在数据框上将此函数与 purrr::map 一起使用。 我的示例数据是: test_data 1 1 2 3
我是 Erlang 新手,因此为了训练,我尝试从头开始实现标准功能。我尝试从 lists 模块创建 map/2 函数的并行实现。但我的实现速度非常慢。如果我在实现中犯了任何主要错误,您能指出我吗:
我是一名优秀的程序员,十分优秀!