![](/img/trans.png)
[英]Passing a function and arguments to a function and purrr
[英]passing ellipsis arguments to map function purrr package, R
我想在 purrr 包的 map 函数中使用省略号参数。 这是一个玩具示例:
f1<-function(x,a=NA,b=NA,prs=seq(0, 1, 0.25),SW=T){
if(SW){
res<-data.frame(name1=a,name2=b,t(quantile(x, prs, na.rm = T)), mean=mean(x, na.rm = T), sd=sd(x, na.rm = T),
NAs=length(x[is.na(x)]),n=length(x[!is.na(x)]),SWp=shapiro.test(x)$p.value,stringsAsFactors =F)
}else
{
res<-data.frame(name1=a,name2=b,t(quantile(x, prs, na.rm = T)), mean=mean(x, na.rm = T), sd=sd(x, na.rm = T),
NAs=length(x[is.na(x)]),n=length(x[!is.na(x)]),stringsAsFactors =F)
}
return(res)
}
f1(c(NA,rnorm(25),NA),SW=F)
f1(c(NA,rnorm(25),NA))
现在我想在另一个函数 f2 中使用 f1:
f2<-function(df,...){
res<-map_df(colnames(df),~f1(df[,.],a=.,...))
return(res)
}
其中 ... 主要用于操作 f1 函数中的 SW 和 a 或 b 参数。 但是 f2 没有做我想要的,可以在这里看到
f2(iris[,-5])
f2(iris[,-5],SW=F)
我感谢任何有关如何使用 addecuatelly ... 内地图的指南
您只需要通过map_df()
调用传递省略号。 否则他们无法进入内部f1()
调用。
f2 <- function(df, ...){
res <- map_df(colnames(df), ~f1(df[,.], a=., ...), ...)
return(res)
}
您还可以在第二个函数的早期捕获省略号,然后使用do.call
将它们添加到您的第一个函数中。 这使得它们的使用地点和使用方式更加明确。
f2 <- function(df, ...){
params <- list(...)
res <- map_df(colnames(df), ~ do.call(
f1, c(list(x = df[,.], a=.), params)))
return(res)
}
MrFlick 解决方案对我不起作用:我认为您确实还需要将...
传递给匿名函数,然后需要使用function(x,...)
而不是~
(如@dmi3kno 所建议的那样)。
这意味着您需要非常令人惊讶的三重...
调用:
map(x, function(x, ...) mean(x, trim=0, ...), ...)
示例:
library(purrr)
x <- list(c(1,2), c(1,2,NA))
fo1 <- function(...) map(x, ~mean(., trim=0, ...), ...)
fo2 <- function(...) map(x, function(x, ...) mean(x, trim=0, ...), ...)
fo1()
#> Warning in if (na.rm) x <- x[!is.na(x)]: the condition has length > 1 and only
#> the first element will be used
#> Warning in if (na.rm) x <- x[!is.na(x)]: the condition has length > 1 and only
#> the first element will be used
#> [[1]]
#> [1] 1.5
#>
#> [[2]]
#> [1] 1.5
fo2()
#> [[1]]
#> [1] 1.5
#>
#> [[2]]
#> [1] NA
fo2(na.rm=TRUE)
#> [[1]]
#> [1] 1.5
#>
#> [[2]]
#> [1] 1.5
由reprex 包(v0.3.0) 于 2020 年 11 月 16 日创建
对于这个问题,我发现rlang::exec()
允许您在与匿名函数结合时将 ... 传递给purrr::map()
,如下所示:
f2 <- function(df, ...){
res <- map(colnames(df), function(x) rlang::exec("f1", df[,x], ...))
return(res)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.