The 27 base and recommended libraries of the standard R 2.12 distribution together contain 3556 functions (you can check using the code posted after the jump). Many of the functions are commonly used: c, data.frame, rnorm, lm. But some of those functions, while being extremely useful, may be less well known to many R users. Some examples I'd wish I'd learned about earlier include tapply, agrep, and formatC. There are also several really useful help pages that aren't associated with specific functions at all, like Syntax and .Machine, that don't get the exposure they deserve.
To help get some of these "hidden gems" of the R documentation better known, we've added a "Function of the Day" section to the home page of inside-R.org. What R functions and help pages do you wish you'd known about earlier? Add a comment to your favorite pages in the Language Reference explaining why they deserve more love, and we'll consider the comments nominations for the Function of the Day.
inside-R.org: Language Reference
# list of base and recommended packages in R 2.12 base.rec <- c( "base","boot","class","cluster","codetools", "datasets","foreign","graphics","grDevices", "grid","KernSmooth","lattice","MASS", "Matrix","methods","mgcv","nlme", "nnet","rpart","spatial","splines", "stats","stats4","survival","tcltk", "tools","utils") n.obj <- rep(0, length(base.rec)) names(n.obj) <- base.rec # count objects in each package for(p in base.rec) { require(p,character.only=TRUE) n.obj[p] <- length( objects(name=paste("package",p,sep=":"))) } # total count sum(base.rec)
Several handy functions are listed here: http://stackoverflow.com/questions/1295955/what-is-the-most-useful-r-trick
Posted by: nico | November 09, 2010 at 12:00
it's not a function, but the '%in%' operator is great, such as:
%in%:
books <- books[books$genre %in% c('Romance', 'Fantasy', 'Science Fiction'),]
... saves multiple == & == & == etc. for referencing columns
all of the file. and dir. commands:
file.create()... dir.create... etc.:
... good for file management and automating various processes.
system():
... good for calling other applications like perl, or another R instance, or anything you can do from a command line.
those are the ones that I can think of off hand.
Posted by: Dan Bowen | November 09, 2010 at 12:23
oh... and,
.Platform
...is great too for cross-platform development. Haven't tested this yet, but... yeah, that's the idea
Posted by: Dan Bowen | November 09, 2010 at 12:26
I think learning about functions that can be used to debug R code have been the biggest boon to my productivity.
dput(): for getting the code to generate an object
str(): compactly print the structure of any object
methods(), getS3method(), and getAnywhere(): to find the code for a generic function
debug(), undebug(), traceback(), and browser(): for function debugging
Posted by: Jeremy Coyle | November 09, 2010 at 13:45
transform, within
Posted by: Kevin Wright | November 09, 2010 at 13:56
David, I think your last line should read:
Posted by: Derek Norton | November 10, 2010 at 05:41
I count 28 rather than 27:
as.character(subset(as.data.frame(installed.packages()),!is.na(Priority))$Package)
gets you the list above ...
I'll put in votes for sweep, scale, subset, match, merge, cumsum/cumprod ...
Posted by: Ben Bolker | November 10, 2010 at 06:45
example() - I wish that was the second function I learned, right after help().
Posted by: Paul Teetor | November 10, 2010 at 11:48
is there a way to see the previous "functions of the day"?
Posted by: Matt G | November 11, 2010 at 13:21
Matt, there's not right now, but it's a great suggestion! I'll see if we can implement that feature for a future update to inside-R.org.
Posted by: David Smith | November 11, 2010 at 13:30
ave() is not well known, but can be very useful
Posted by: Adam | January 08, 2011 at 12:09
My own functions are my favorite. :)
here's one I especially find useful
getfirsts <- function(data, searchcol){
# Receives a data frame and a search column in that data frame.
# Returns a data frame of the first occurances of all unique values of of the
# "search" column.
rows <- as.data.frame(match(unique(data[[searchcol]]), data[[searchcol]]))
firsts = data[rows[[1]],]
return(firsts)
}
Posted by: Nathan G | December 16, 2012 at 13:08