There are many ways to load datasets in R. Here are a few options you can consider.
Load Datasets In R
Load a built-in dataset
The default installation of R comes with the Datasets package, which provides a wide range of built-in datasets you can load and use right away.
For example, iris is the name in R of the Fisher’s Iris data set. It contains information, such as sepal length, about 50 samples from each of three species of Iris.
You can use the data() function to list all of the datasets that are included with R and then use the data() function again to load a specific dataset. To load this dataset, use this command:
Most of the time, the datasets package has already been loaded when you start an R environment. When this isn’t the case, the data() will fail to find the dataset and give you this error:
In data("iris") : data set 'iris' not found
When this happens, use the library() function to load the datasets package first:
Note: learn more about loading packages in R.
When this dataset has been successfully loaded, you can begin manipulating and analyzing it with all valid functions in R.
For example, you can use the summary() function to produce a quick summary of this dataset:
> summary("iris") Length Class Mode 1 character character
Load a dataset from a file
You can use the read.table() function to read a dataset from a file and store it in a data frame.
For example, to read a CSV file called data.csv and store it in a data frame called myData, you can use the following code:
myData <- read.table("data.csv", sep=",", header=TRUE)
In the above command, the sep argument indicates the separator character that read.table() will use to parse your data source. By default, it uses white space (spaces, newlines, and tabs), but you can set it to other common separator characters like commas.
The read.table() function can’t just read files in a local filesystem but can also access files over a network, such as the internet.
Using this capability, you can read a dataset from a URL and store it in a data frame. For example, to read a CSV file stored at the URL
https://learnshareit.com/data.csv and store it in a data frame called myData, you can use the following code:
myData <- read.table("https://learnshareit.com/data.csv", sep=",", header=TRUE)
Load a dataset from a database
In addition to text files, R also supports databases like MySQL through its third-party packages.
RMySQL used to be the most popular option for connecting to MySQl databases and retrieving your data, but it is no longer maintained. RMariaDB has been developed as an alternative to it.
To use this driver, you will need to install it first:
You can then load it and the built-in DBI library to start a connection to a database:
library(RMariaDB) library(DBI) con <- dbConnect(RMariaDB::MariaDB(), group = "learnshareit")
Depending on your source, you can load datasets in R in different ways. This language supports a wide range of data sources, from simple text files to common database management systems.
My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Name of the university: HUST