In R, how to use a "null" default value for an argument of a function?

Viewed 15724

Here is my current code:

my.function <- function( my.arg="" ){
  if(my.arg == "")
    my.arg <- rnorm(10)
  return( mean(my.arg) )
}

It returns me this:

> my.function( rbinom(10, 100, 0.2) )
[1] 18.5
Warning message:
In if (a == "") a <- rnorm(10) :
  the condition has length > 1 and only the first element will be used

I tried with my.arg=c(), or my.arg=0, but I always get either a warning or an error. And the R manual doesn't say much on this issue.

Any idea? Thanks in advance!

3 Answers
Related