调用另一个函数的函数的参数

时间:2022-06-02 18:28:54

Consider a hypothetical example:

考虑一个假想的例子:

sim <- function(n,p){
  x <- rbinom(n,1,p)
  y <- (x==0) * rnorm(n)
  z <- (x==1) * rnorm(n,5,2)

  dat <- data.frame(x, y, z)
  return(dat)
}

Now I want to write another function simfun where I will call the above sim function and check if y and z columns of the data frame is less than a value k.

现在我想写另一个函数simfun我将调用上面的sim函数检查数据框的y和z列是否小于k。

simfun <- function(n, p, k){
   dat <- sim(n, p)
   dat$threshold <- (dat$y<=k & dat$z<=k)

  return(dat$threshold)
}

But is it standard to use the argument of sim as the argument of simfun? Can I write simfun <- function(k) and call the sim function inside simfun?

但是用sim的参数作为simfun的参数是标准的吗?我可以写simfun <- function(k)并调用simfun中的sim函数吗?

2 个解决方案

#1


2  

I'd say it's fairly standard to do this sort of thing in R. A few pointers to consider:

我想说,在r中做这种事是相当标准的。

Usually you should explicitly declare the argument names so as not to create any unwanted behaviour if changes are made. I.e., instead of sim(n, p), write sim(n = n, p = p).

通常,您应该显式地声明参数名称,以便在发生更改时不创建任何不需要的行为。即。,而不是sim(n, p),写sim(n = n, p = p)。

To get simfun() down to just a k argument will require default values for n and p. There are lots of ways to do this. One way would be to hardcode inside simfun itself. E.g.:

为了使simfun()降到只有k个参数,需要n和p的默认值。一种方法是在simfun内部硬编码。例如:

simfun <- function(k) {
  dat <- sim(n = 100, p = c(.4, .6))
  dat$threshold <- (dat$y<=k & dat$z<=k)

  return(dat$threshold)
}
simfun(.5)

A more flexible way would be to add default values in the function declaration. When you do this, it's good practice to put variables with default values AFTER variables without default values. So k would come first as follow:

更灵活的方法是在函数声明中添加默认值。当您这样做时,最好将具有默认值的变量放在没有默认值的变量之后。所以k首先是,

simfun <- function(k, n = 100, p = c(.4, .6)){
  dat <- sim(n, p)
  dat$threshold <- (dat$y<=k & dat$z<=k)

  return(dat$threshold)
}
simfun(.5)

The second option is generally preferable because you can still change n or p if you need to.

第二种选择通常更可取,因为如果需要,你仍然可以改变n或p。

#2


0  

While not great, you could define n and p separately

虽然不是很好,但是可以分别定义n和p

n <- 1
p <- .5

simfun <- function(k){
  dat <- sim(n, p)
  dat$threshold <- (dat$y<=k & dat$z<=k)
return(dat$threshold)
}

You can read more about R Environments here: http://adv-r.had.co.nz/Environments.html

您可以在这里阅读更多关于R环境的信息:http://adv-r.had.co.nz/Environments.html

#1


2  

I'd say it's fairly standard to do this sort of thing in R. A few pointers to consider:

我想说,在r中做这种事是相当标准的。

Usually you should explicitly declare the argument names so as not to create any unwanted behaviour if changes are made. I.e., instead of sim(n, p), write sim(n = n, p = p).

通常,您应该显式地声明参数名称,以便在发生更改时不创建任何不需要的行为。即。,而不是sim(n, p),写sim(n = n, p = p)。

To get simfun() down to just a k argument will require default values for n and p. There are lots of ways to do this. One way would be to hardcode inside simfun itself. E.g.:

为了使simfun()降到只有k个参数,需要n和p的默认值。一种方法是在simfun内部硬编码。例如:

simfun <- function(k) {
  dat <- sim(n = 100, p = c(.4, .6))
  dat$threshold <- (dat$y<=k & dat$z<=k)

  return(dat$threshold)
}
simfun(.5)

A more flexible way would be to add default values in the function declaration. When you do this, it's good practice to put variables with default values AFTER variables without default values. So k would come first as follow:

更灵活的方法是在函数声明中添加默认值。当您这样做时,最好将具有默认值的变量放在没有默认值的变量之后。所以k首先是,

simfun <- function(k, n = 100, p = c(.4, .6)){
  dat <- sim(n, p)
  dat$threshold <- (dat$y<=k & dat$z<=k)

  return(dat$threshold)
}
simfun(.5)

The second option is generally preferable because you can still change n or p if you need to.

第二种选择通常更可取,因为如果需要,你仍然可以改变n或p。

#2


0  

While not great, you could define n and p separately

虽然不是很好,但是可以分别定义n和p

n <- 1
p <- .5

simfun <- function(k){
  dat <- sim(n, p)
  dat$threshold <- (dat$y<=k & dat$z<=k)
return(dat$threshold)
}

You can read more about R Environments here: http://adv-r.had.co.nz/Environments.html

您可以在这里阅读更多关于R环境的信息:http://adv-r.had.co.nz/Environments.html