R中功能多态性的建议做法是什么? [英] What are the suggested practices for function polymorphism in R?

查看:66
本文介绍了R中功能多态性的建议做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在R中编写一个函数,该函数是对某些数据的几个足够统计信息的函数.例如,假设函数调用它foo.func仅取决于数据样本的样本均值.为了方便起见,我认为用户可能希望将随机变量的样本(在这种情况下,foo.func计算样本均值),样本平均值传递给foo.func,这就是所有foo.func需要.出于效率考虑,如果调用多个函数(例如foo.func)可以采用样本均值,则后者是首选.在那种情况下,均值仅需计算一次(在我遇到的实际问题中,所涉及的样本统计数据可能需要大量计算).

Suppose I want to write a function in R which is a function of a couple of sufficient statistics on some data. For example, suppose the function, call it foo.func depends only on the sample mean of a sample of data. For convenience, I think users might like to pass to foo.func the sample of random variables (in which case foo.func computes the sample mean), or the sample mean itself, which is all that foo.func needs. For reasons of efficiency, the latter is preferred if there are multiple functions like foo.func being called which can take the sample mean. In that case the mean need only be computed once (in the real problem I have, the sample statistics in question might be computationally intensive).

总之,我想编写foo.func以便初学者可以访问(传递数据,让函数计算足够的统计信息)以及专家(预先计算足够的统计信息以提高效率并将它们传递给).建议的做法是什么?我是否传递了逻辑标志?有多个论点?某些方法可能是:

In summary, I would like to write foo.func to be accessible to the beginner (pass in the data, let the function compute the sufficient statistics) as well as the expert (precompute the sufficient statistics for efficiency and pass them in). What are the recommended practices for this? Do I have a logical flag passed in? Multiple arguments? Some ways to do it might be:

#optional arguments
foo.func <- function(xdata, suff.stats=NULL) {
  if (is.null(suff.stats)) {
    suff.stats <- compute.suff.stats(x)
  }
  #now operate on suff.stats
}

#flag input
foo.func <- function(data.or.stat, gave.data=TRUE) {
  if (gave.data) {
    data.or.stat <- compute.suff.stats(data.or.stat)
  }
  #now operate on data.or.stat
}

我认为我倾向于前者

推荐答案

您还可以将函数嵌入到参数中,如下所示:

You can also embed functions into the arguments, as:

foo.func <- function(x, suff.stats = foo.func.suff.stat(x)){
  # your code here
}

例如:

foo.func <- function(x, avg = mean(x)){
  return(avg)
}

foo.func(1:20)
foo.func(avg = 42)

或者,您可以为各种参数使用默认设置NULL,并测试is.null(argument),或者为您可能计算出的每个参数简单地检查missing(argument)的值.

Alternatively, you can either use a default setting of NULL for various arguments, and test for is.null(argument), or simply check the value of missing(argument) for each for each argument you might calculate.

更新1:我建议使用默认值NA是错误的:使用NULL更合适.对于矢量输入,使用NAis.na()的行为会很奇怪,而NULL只是一个对象-无法创建一个带有NULL值的矢量,因此is.null(argument)的行为符合预期.为健忘而道歉.

Update 1: I erred in suggesting use of a default value of NA: it is far more appropriate to use NULL. Using NA and is.na() will behave oddly for vector inputs, whereas NULL is just a single object - one cannot create a vector of NULL values, so is.null(argument) behaves as expected. Apologies for the forgetfulness.

这篇关于R中功能多态性的建议做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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