在这个问题Name output of pmap on tibble中,我询问如何正确命名调用的输出pmap
并得到了一个很好的答案,我认为我理解了。
然而,当我进一步研究这种行为时,我不再确定我是否理解其内在原理:
library(purrr)
library(tibble)
params <- tibble(nm = LETTERS[1:2], x = 1:2, y = 2:3, z = 1:2)
## 1. setting names on the first argument gives the expected results
params %>%
mutate(nm = set_names(nm)) %>%
pmap(function(nm, x, y, ...) {
x + y
}) %>%
names()
# [1] "A" "B"
## 2. actually it does not need to be the first element
params %>%
mutate(nm = set_names(nm)) %>%
pmap(function(x, y, nm, ...) {
x + y
}) %>%
names()
# [1] "A" "B"
## 3. however, setting names on `x` does not work
params %>%
mutate(x = set_names(x, nm)) %>%
pmap(function(x, y, nm, ...) {
x + y
}) %>%
names()
# NULL
## 4. I thought maybe because they disappear during the addition `+`, but nope
params %>%
mutate(z = set_names(z, nm)) %>%
pmap(function(x, y, nm, ...) {
x + y
}) %>%
names()
# NULL
## 5. Ok maybe a catch all argument `...` prohibits this behaviour, but no
params %>%
mutate(z = set_names(z, nm)) %>%
pmap(function(x, y, nm, z) {
x + y
}) %>%
names()
# NULL
## 6. It seems that names on a character vector work even if not referenced directly
params %>%
mutate(nm = set_names(nm)) %>%
pmap(function(x, y, ...) {
x + y
}) %>%
names()
# [1] "A" "B"
有人可以告诉我电话中的名字最终是如何确定的吗[p]map
?
答案取自我对原始问题的评论。
名称取自您的第一列
tibble/data.frame
(在结构上是向量列表)并应用于结果列表(调用函数)。所以它几乎与函数本身如何计算结果无关。
步骤是
pmap
),否则从x