What is the data() function in R?

There are more than 100 datasets available in R, included in the datasets package. To see the list of available datasets, use data() function. All available datasets in R can be accessed by their explicit names. This function also helps to access build-in datasets from other R packages (special module-based prebuild datasets).

Syntax

This method has the following syntax, including some optional parameters.


data(...,
    list = character(), # optional
    package = NULL, # optional
    verbose = getOption("verbose"),  # optional         
    envir = .GlobalEnv, # optional
    lib.loc = NULL, # optional
    overwrite = TRUE) # optional

Parameters

  • : These are names or strings.
  • list, default= character(): This is a character vector.
  • package, default= NULL: This is a vector to give the name of a package to load a particular dataset.
  • lib.loc, default= NULL: This is a set of directory names of R libraries. If set to NULL, data() has excell to all libraries.
  • verbose, default= getOption("verbose"): If this is set to logical True, additional dataset info is printed.
  • envir, default= .GlobalEnv: This is the tool or environment where the dataset will be loaded.
  • overwrite, default = TRUE: This is used to replace names of different objects with the same name.

Return values

This function returns a character vector of a specified dataset.

Explanation

Now, let’s discuss the data() function with a code example.

# program to load directly available datasets
# invoking data() function from core R
data("iris")
# show first 8 rows of iris dataset
head(iris, 8)
  • Line 3: We load the iris dataset in our program.
  • Line 5: We print eight entries of the iris dataset on the console.
# program to load directly available datasets
# invoking data() function from core R
data(infert)
# show first eight observations from infert dataset
head(infert,8)

This dataset infert has a dimension of 8 columns and 248 rows, which contains information about infertility based on gender, age, etc.

  • Line 3: data(infert) will load infert dataset in this program.
  • Line 5: It will print the first eight observations on the console.
# Loading
data(mtcars)
# Print the first 6 rows
head(mtcars)

This mtcars dataset contains information about motors: their design, fuel consumption, and performance. It has 11 columns and 32 rows in detail.

  • Line 2: We load the mtcars dataset in this program.
  • Line 4: The head() function will print the top six entries from the dataset.