为什么函数参数名称需要与pmap匹配列表名称?

问题描述 投票:1回答:1

我试图理解此代码为何起作用:

tmp <- list(this = list(1:5), 
            that = list(10*c(1:5)), 
            other = list(100*c(1:5))) 

tmp %>% pmap(function(this, that, other) paste(this, that, other))

...但是以下代码会产生“未使用的参数”错误:

tmp %>% pmap(function(a, b, c) paste(a, b, c))

[我遇到了this GitHub帖子,但是我仍然不清楚它如何应用..谢谢!

r purrr
1个回答
0
投票

除非我误解了您的问题,否则第二个将不起作用,因为abc不是pmap调用环境中附加的列表元素。

如果您将列表元素命名为abc,则可以使用。

tmp2 <- list(a = list(1:5), 
            b = list(10*c(1:5)), 
            c = list(100*c(1:5))) 

tmp2 %>% pmap(function(a, b, c) paste(a, b, c))
#[[1]]
#[1] "1 10 100" "2 20 200" "3 30 300" "4 40 400" "5 50 500"
© www.soinside.com 2019 - 2024. All rights reserved.