Pages

Thursday, August 08, 2013

Google Developers R Programming Video Lectures

I got this Google Developers R Programming Video Lectures from Stephen's blog - Getting Genetics Done.
Very useful R tutorial for beginner! Short and efficient. 
Here is what I learned after watching the lectures:
stop() and warning() function
I 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.

To return index of an array/dataframe, use which(df, arr.ind=T), e.g. which(is.na(df), arr.ind=T) will return the column/row index of NA elements.
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));
}

No comments:

Post a Comment