Getting Help with R Programming: Useful Survival Skills
February 23, 2013 1 Comment
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
and you will be directed to the relevant documentation for that function. For example, if you need help with the lm() function, type
*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')  ".__C__mtable" ".__C__summary.table"  ".__C__table" ".__S3MethodsTable__."  "aperm.table" "as.data.frame.table"  "as.relistable" "as.table"  "as.table.default" "ftable"  "is.relistable" "is.table"  "margin.table" "model.tables"  "pairwise.table" "print.ftable"  "print.summary.table" "print.table"  "prop.table" "r2dtable"  "read.ftable" "read.table"  "summary.table" "table"  "write.ftable" "write.table"  "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.)
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)  "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!