Getting Help with R Programming: Useful Survival Skills

Useful Resources to Learn about R on the Internet

When I program in R and struggle with something, the first thing that I usually turn to is Google.  I search the relevant function or the desired outcome, and I often find the solutions within the first few hits.  They likely show up in the documentation, online discussion forums like Nabble and Stack Overflow, email threads within the R Mailing List, and web sites that provide tutorials on R (like R-Bloggers and the UCLA Statistical Computing Group).  These are all great resources.

Today, however, I’m going to show you some useful survival skills within the R environment.  I read about these help facilities on Page 7 of the 3rd edition of the book “Data Analysis and Graphics Using R” by John Maindonald and W. John Braun.

Useful Help Faciliities within R

1.  If you need help with a known function, type

?function 

or

help(function),

and you will be directed to the relevant documentation for that function.  For example, if you need help with the lm() function, type

?lm

or

help(lm).  

*By the way, I encourage you to read the documentation for the help() function!  Look for it in R on your own!

2.  Have you ever needed to use a function but did not know its exact name or were not sure of its existence?  Did you ever have a hunch that a function should exist based on your knowledge of a related function?  I was once in such a situation with the data.table() function.  There is a very useful function called apropos(‘argument‘) that lists all functions that contain your argument as part of their names.  Note that your argument must be put within either single or double quotation marks.  For example, here is what I got when I looked for similar functions containing the string ‘table’.

> apropos('table')
 [1] ".__C__mtable"         ".__C__summary.table" 
 [3] ".__C__table"          ".__S3MethodsTable__."
 [5] "aperm.table"          "as.data.frame.table" 
 [7] "as.relistable"        "as.table"            
 [9] "as.table.default"     "ftable"              
[11] "is.relistable"        "is.table"            
[13] "margin.table"         "model.tables"        
[15] "pairwise.table"       "print.ftable"        
[17] "print.summary.table"  "print.table"         
[19] "prop.table"           "r2dtable"            
[21] "read.ftable"          "read.table"          
[23] "summary.table"        "table"               
[25] "write.ftable"         "write.table"         
[27] "xyTable"

Note that the argument is a string, so it does not need to be an actual word or name of a function.  For example, apropos(‘tabl’) will return the same results.  Try it!

3. There may be other times when you want to learn about all functions involving a certain term, but searching for R-related pages on that term returns too many irrelevant results.  This term may be not even be an R function or command, making the Google search all the more difficult, even with good searching techniques.  In these situations, use the help.search(argument) function.  (Again, you need to put your arguments around single or double quotation marks.)  This will return all functions with your argument in the help page title or as an alias.  

For example, I wanted know about using PDF files in R.  I ran help.search(‘pdf’) in R and got the following results.  (I was running R in my UNIX Terminal.  You may get these same results in another interface.  For example, you may get a new window with a point-and-click interface of these results if you are running R for Mac OS Gui.)

Screen Shot 2013-02-22 at 1.10.42 PM

Now, if you are interested in reading the documentation of one of these functions, use the ?function or help(function) as mentioned above.

4. Sometimes, it’s easier to learn how to use a function by looking at an example rather than following the documentation.  If you want to see some examples of how a function works, use the example() function.  Beware, however, that some of the examples may be rather advanced, even for basic functions.  You will need to spend some time to digest all of them if you want to fully understand those examples.

For instance, if you want to learn how the colnames() function works, here is what you will get:

> example(colnames)

colnms> m0 <- matrix(NA, 4, 0)

colnms> rownames(m0)
NULL

colnms> m2 <- cbind(1,1:4)

colnms> colnames(m2, do.NULL = FALSE)
[1] "col1" "col2"

colnms> colnames(m2) <- c("x","Y")

colnms> rownames(m2) <- rownames(m2, do.NULL = FALSE, prefix = "Obs.")

colnms> m2
      x Y
Obs.1 1 1
Obs.2 1 2
Obs.3 1 3
Obs.4 1 4

If you have any other useful survival skills for R, please share them in the comments!

One Response to Getting Help with R Programming: Useful Survival Skills

Your thoughtful comments are much appreciated!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: