Get List of Data Sets in Package in R (Example)
In this article, I’ll illustrate how to return a list of data sets in a particular package in the R programming language.
The tutorial looks as follows:
Let’s do this!
Example: Return All Data Sets Contained in a Package Using data() Function
This section shows how to get a list of all data sets that are provided by a certain add-on package.
For this example, we’ll use the ggplot2 package. We first have to install and load ggplot2:
install.packages("ggplot2") # Install & load ggplot2 library("ggplot2")
In the next step, we can apply the data() function to create a list of data sets that are contained in ggplot2:
data_list <- data(package = "ggplot2")$results[ , "Item"] # Create list of packages data_list # Print list of packages #  "diamonds" "economics" "economics_long" "faithfuld" "luv_colours" "midwest" "mpg" "msleep" "presidential" "seals" "txhousing"
As you can see, the ggplot2 package provides many different data sets. One of them is the diamonds data set.
We can load this data set as shown below:
data(diamonds) # Load diamonds data set head(diamonds) # Print head of diamonds data set
By executing the previous R programming syntax, we have returned the first six rows of the diamonds data set.
Video & Further Resources
If you need further explanations on the R code of this article, I can recommend having a look at the following video on my YouTube channel. I’m explaining the R codes of this article in the video:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you could have a look at the other tutorials on my website. I have published several tutorials about related topics such as variables, lists, and data elements.
- Get Column Index in Data Frame by Variable Name
- Create Data Frame where a Column is a List
- Get Row Index Number in R
- Get Value of Data Element without Name or Index
- All R Programming Tutorials
In this tutorial you have learned how to print a list of data sets in a certain package in the R programming language. In case you have any further comments and/or questions, tell me about it in the comments below. Furthermore, please subscribe to my email newsletter to receive updates on the newest articles.
Statistics Globe Newsletter