The R package, in addition to code and functions, may contain datasets that support any of the R designated formats (data frame, time series, matrix, and so on). In most cases, the use of the dataset is either related to the package's functionalities or for educational reasons. For example, the TSstudio package, which stores most time series datasets, will be used in this book. In the following example, we will load the US total monthly vehicle sales again, this time using the TSstudio package:
# If the package is not installed on your machine:
install.packages("TSstudio")
# Loading the series from the package
data("USVSales", package = "TSstudio")
The class(USVSales) function gives us the following output:
class(USVSales)
## [1] "ts"
The head(USVSales) function gives us the following output:
head(USVSales)
## [1] 885.2 994.7 1243.6 1191.2 1203.2 1254.7
Note that the data function is not assigning the object. Rather, it is loading it to the environment from the package. The main advantage of storing a dataset in a package format is that there is no loss of attributes (that is, there is no difference between the original object and the loaded object).
We used the data function to load the USVSales dataset of the TSstudio package. Alternatively, if you wish to assign the dataset to a variable, you can do either of the following:
- Load the data into the working environment and then assign the loaded object to a new variable.
- Assign directly from the package to a variable by using the :: operator. The :: operator allows you to call for objects from a package (for example, functions and datasets) without loading it into the working environment. For example, we can load the USVSales dataset series directly from the TSstudio package with the :: operator:
US_V_Sales <- TSstudio::USVSales
Note that the USVSales dataset series that we loaded from the TSstudio package is a time series (ts) object, that is, a built-in R time series class. In Chapter 3, The Time Series Object, we will discuss the ts class and its usage in more detail.