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!
First time, function is called with single argument and default value is used for second argument. Function called successfully and produce a result. Second time, function is called with two arguments, default value of second argument is overridden.
Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.
Any number of arguments in a function can have a default value.
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.
try
my.function <- function( my.arg=NULL ){
if(is.null(my.arg)) ...
There's also missing
:
my.function <- function(my.arg) {
if(missing(my.arg)) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With