将省略号参数传递给映射函数 purrr 包,R [英] passing ellipsis arguments to map function purrr package, R

查看:52
本文介绍了将省略号参数传递给映射函数 purrr 包,R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 purrr 包的 map 函数中使用省略号参数.这是一个玩具示例:

I want to use ellipsis parameters inside map function of purrr package. this is a toy example:

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:

now I want to use f1 inside another function f2:

f2<-function(df,...){
  res<-map_df(colnames(df),~f1(df[,.],a=.,...))
  return(res)
}

where ... 主要用于操作 f1 函数中的 SW 和 a 或 b 参数.但是 f2 没有做我想做的事情,可以在这里看到

where ... is intended mainly to manipulate SW and a or b parameters in f1 function. however f2 is not doing what I want as can be seen here

f2(iris[,-5])
f2(iris[,-5],SW=F)

我感谢任何关于如何使用 addecuatelly ... 内地图的指南

I appreciate any guide in how to use addecuatelly ... inside map

推荐答案

您只需要通过 map_df() 调用传递省略号.否则他们无法进入内部的 f1() 调用.

You just need to pass the ellipses through the map_df() call as well. Otherwise they can't get into the inner f1() call.

f2 <- function(df, ...){
  res <- map_df(colnames(df), ~f1(df[,.], a=., ...), ...)
  return(res)
}

这篇关于将省略号参数传递给映射函数 purrr 包,R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆