The library()
function is used to load libraries, or groups of functions and data sets that are not included
in the base R distribution. Basic functions
that perform least squares linear regression and other simple analyses come
standard with the base distribution, but more exotic functions require additional libraries. Here we load
the MASS
package, which is a very large collection of data sets and functions. We also load the ISLR2
package, which
includes the data sets associated with this book.
library(MASS)
library(ISLR2)
If you receive an error message when loading any of these libraries, it
likely indicates that the corresponding library has not yet been installed
on your system. Some libraries, such as MASS
, come with R and do not need to
be separately installed on your computer. However, other packages, such as ISLR2
, must be downloaded the
first time they are used. This can be done directly from within R. For example, on a Windows system, select the Install
package
option under the Packages
tab. After you select any mirror site, a
list of available packages will appear. Simply select the package you wish to
install and R will automatically download the package. Alternatively, this
can be done at the R command line via install.packages("ISLR2")
. This installation only needs to be done the first time you use a package. However,
the library()
function must be called each time you wish to use a given
package.
install.packages('MASS')
install.packages('ISLR2')
library(MASS)
library(ISLR2)