time
time

Reputation: 949

Arguments of a function where another function will be called

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 <- 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?

Upvotes: 3

Views: 57

Answers (2)

vitallish
vitallish

Reputation: 322

While not great, you could define n and p separately

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

Upvotes: 0

Simon Jackson
Simon Jackson

Reputation: 3174

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

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).

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 <- 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:

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.

Upvotes: 2

Related Questions