Very useful R tutorial for beginner! Short and efficient.
Here is what I learned after watching the lectures:
stop() and warning() functionI was asked this question during a job interview. stop('message') will print out the error message and stop the function. warning('message') will print out the error message but continue the function.
Passing additional arguments using an ellipsis(...), for example:
myFunc <- function(a, ...){
return colMean(a, ...);
}
will allow us to call the function like myFun(a, na.rm=T), for example.
You can also record the ellipsis arguments by
args=list(...)
return() vs. invisible()
return() will return the values and print out in the screen.
invisible will return the values but not print out to the screen.
use recall() to call recursive function in R. For example, instead of writing recursive function like
myFunc <- function(a)
{
return(if(a>1, myFunc(log(a)), a));
}
, write it as
myFunc <- function(a)
{
return(if(a>1, recall(log(a)), a));
}
return() vs. invisible()
return() will return the values and print out in the screen.
invisible will return the values but not print out to the screen.
use recall() to call recursive function in R. For example, instead of writing recursive function like
myFunc <- function(a)
{
return(if(a>1, myFunc(log(a)), a));
}
, write it as
myFunc <- function(a)
{
return(if(a>1, recall(log(a)), a));
}
No comments:
Post a Comment