R Packages

From time to time, one’s needs may exceed the capacities of the basic form of R, called base R. At such times, one may need to download and use additional components called packages.

Useful Commands

getOption("defaultPackages"): Lists the packages that are loaded automatically by R.

(.packages()): Lists the packages that are currently loaded and, therefore, ready for use.

The command (.packages()) above without the outer parentheses generates the list but does not print it out on the screen. One can use the command to create an object – in this case a character vector – that contains the list, like so: ListOfCurrentlyLoadedPackages <- .packages(). This object could then be used for other purposes.

.packages(all.available = TRUE): Lists the packages that are currently installed and, therefore, ready to be loaded and then used.

installed.packages(): Produces a similar list, usually a very long one.

install.packages("mypackage"): Installs a package called mypackage, if it exists.

if (!"mypackage" %in% rownames(installed.packages())) {install.packages("mypackage")}: Installs a package after making sure it has not been installed already.

library(mypackage): This loads a previously installed package for use.

require(mypackage): Same as library(mypackage).

remove.packages("mypackage"): This uninstalls a package.

data(package = "mypackage"): This lists the datasets that come with a package.

Some Important R Packages

“AER”, “car”, “censReg”, “dummies”, “dynlm”, “effects”, “ggplot2”, “lmtest”, “maps”, “mfx”, “orcutt”, “pdfetch”, “plm”, “sandwich”, “quantreg”, “rmarkdown”, “sampleSelection”, “stargazer”, “survival”, “systemfit”, “truncreg”, “tseries”, “urca”, “xtable”, “vars”, “xts”, “zoo”.