R is a statistical software made up of many user-written packages. The base version of R that is downloaded allows the user to get started in R, but anyone performing data analysis will quickly exhaust the capabilities of base R and need to install additional packages. Here are some basic commands for managing R packages.
Which packages do I already have?
To see what packages are installed, use the installed.packages() command. This will return a matrix with a row for each package that has been installed. Below, we look at the first 5 rows of this matrix.
installed.packages()[1:5,] Package LibPath Version Priority base "base" "C:/PROGRA~1/r/R-211~1.1/library" "2.11.1" "base" boot "boot" "C:/PROGRA~1/r/R-211~1.1/library" "1.2-42" "recommended" car "car" "C:/PROGRA~1/r/R-211~1.1/library" "2.0-2" NA class "class" "C:/PROGRA~1/r/R-211~1.1/library" "7.3-2" "recommended" cluster "cluster" "C:/PROGRA~1/r/R-211~1.1/library" "1.12.3" "recommended" Depends Imports LinkingTo base NA NA NA boot "R (>= 2.9.0), graphics, stats" NA NA car "R (>= 2.1.1), stats, graphics, MASS, nnet, survival" NA NA class "R (>= 2.5.0), stats, utils" "MASS" NA cluster "R (>= 2.9.0), stats, graphics, utils" NA NA Suggests Enhances OS_type License Built base NA NA NA "Part of R 2.11.1" "2.11.1" boot "survival" NA NA "Unlimited" "2.11.1" car "alr3, leaps, lmtest, sandwich, mgcv, rgl" NA NA "GPL (>= 2)" "2.11.1" class NA NA NA "GPL-2 | GPL-3" "2.11.1" cluster NA NA NA "GPL (>= 2)" "2.11.1"
From this output, we will first focus on the Package and Priority columns. The Package column gives the name of the package and the Priority column indicates what is needed to use functions from the package.
- If Priority is "base", then the package is already installed and loaded, so all of its functions are available upon opening R.
- If Priority is "recommended", then the package was installed with base R, but not loaded. Before using the commands from this package, the user will have to load it with the library command, e.g. library(boot).
- If Priority is NA, then the package was installed by the user, but not loaded. Before using the commands from this package, the user will have to load it with the library command, i.e., library(car).
Have I installed a specific package?
Sometimes, you might want to know if you have already installed a specific
package. Let’s say we want to check if we have installed the package "boot".
Instead of checking the entire list of installed packages, we can do the
a<-installed.packages() packages<-a[,1] is.element("boot", packages) TRUE
How can I add or delete packages?
Any package that does not appear in the installed packages matrix must be installed and loaded before its functions can be used. A package can be installed using install.packages("<package name>"). A package can be removed using remove.packages("<package name>").
What packages are available?
The list of available R packages is constantly growing. The actual list can be obtained using available.packages(). This returns a matrix with a row for each package.
p <- available.packages() dim(p)  2553 12 p[1:5,] Package Version Priority Depends Imports ACCLMA "ACCLMA" "1.0" NA NA NA ADGofTest "ADGofTest" "0.1" NA NA NA AER "AER" "1.1-7" NA "R (>= 2.5.0), stats, car (>= 2.0-1), Formula (>= 0.2-0),nlmtest, sandwich, strucchange, survival, zoo" "stats" AGSDest "AGSDest" "1.0" NA "ldbounds" NA AICcmodavg "AICcmodavg" "1.11" NA NA NA LinkingTo ACCLMA NA ADGofTest NA AER NA AGSDest NA AICcmodavg NA Suggests ACCLMA NA ADGofTest NA AER "boot, dynlm, effects, foreign, ineq, KernSmooth, lattice,nMASS, mlogit, nlme, nnet, np, plm, pscl, quantreg, ROCR,nsampleSelection, scatterplot3d, systemfit, rgl, truncreg,ntseries, urca" AGSDest NA AICcmodavg "lme4, MASS, nlme, nnet" Enhances OS_type License File Repository ACCLMA NA NA "GPL-2" NA "http://cran.stat.ucla.edu/bin/windows/contrib/2.11" ADGofTest NA NA "GPL" NA "http://cran.stat.ucla.edu/bin/windows/contrib/2.11" AER NA NA "GPL-2" NA "http://cran.stat.ucla.edu/bin/windows/contrib/2.11" AGSDest NA NA "GPL (>= 2)" NA "http://cran.stat.ucla.edu/bin/windows/contrib/2.11" AICcmodavg NA NA "GPL (>= 2 )" NA "http://cran.stat.ucla.edu/bin/windows/contrib/2.11"
These first five (of 2,553) available packages illustrate that the package names are often acronyms and rarely reveal what the package functions do. A list of the packages available through CRAN including a short package description can be found at CRAN’s Contributed Packages page.
What functions and datasets are in a package?
It is easy to access some quick documentation for a package from R with the help command. This opens an R window with package information followed by a list of functions and datasets.
Once a package is loaded, the help command can also be used with all functions and datasets listed here, e.g. help(Null).