What is data("dataset") in R good for?

Viewed 65

The help page of the data function says that it can be used to load a dataset. It seems however that all datasets defined in packages are already available whether data is called or not. Example:

> libary(MASS)
> names(quine)
[1] "Eth"  "Sex"  "Age"  "Lrn"  "Days"
> data("quine")
> names(quine)
[1] "Eth"  "Sex"  "Age"  "Lrn"  "Days"

Even datasets in unloaded packages are available without call of data:

> names(boot::acme)
[1] "month"  "market" "acme"

So what is data("dataset") good for and when is it necessary?

2 Answers

There are cases that's it's not needed, cases that it's useful and cases that is required.

  1. For those cases that we don't have any conflict of names (object, other package's dataset or even functions) it's not really needed to load a dataset with data:
library(ggplot2)

diamonds
## A tibble: 53,940 x 10
#   carat cut       color clarity depth table price     x     y     z
#   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
# 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
# 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
# 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
# 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
# 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
# 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
# 7 0.24  Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47
# 8 0.26  Very Good H     SI1      61.9    55   337  4.07  4.11  2.53
# 9 0.22  Fair      E     VS2      65.1    61   337  3.87  3.78  2.49
#10 0.23  Very Good H     VS1      59.4    61   338  4     4.05  2.39
# … with 53,930 more rows
  1. For those cases that it's useful, we can use it just for code readability, convention, by having some information about it, or even to assign it to a new environment.
library(ggplot2)

data(diamonds, verbose = TRUE, envir = e <- new.env())

#name=diamonds:  NOT found in names() of Rdata.rds, i.e.,
#   gss_cat

#name=diamonds:  NOT found in names() of Rdata.rds, i.e.,
#   fruit,sentences,words

#name=diamonds:  NOT found in names() of Rdata.rds, i.e.,
#   band_instruments,band_instruments2,band_members,starwars,storms

#name=diamonds:  NOT found in names() of Rdata.rds, i.e., 
#billboard,construction,fish_encounters,population,relig_income,smiths,tab 
#le1,table2,table3,table4a,table4b,table5,us_rent_income,who,world_bank_pop

#name=diamonds:  found in Rdata.rds
  1. And finally, for those cases that it's required is when we have objects with the same name and we can't call the dataset without function data:
library(ggplot2)

diamonds <- c(1, 2, 3, 4)
diamonds
#[1] 1 2 3 4

data(diamonds)
diamonds

## A tibble: 53,940 x 10
#   carat cut       color clarity depth table price     x     y     z
#   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
# 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
# 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
# 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
# 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
# 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
# 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
# 7 0.24  Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47
# 8 0.26  Very Good H     SI1      61.9    55   337  4.07  4.11  2.53
# 9 0.22  Fair      E     VS2      65.1    61   337  3.87  3.78  2.49
#10 0.23  Very Good H     VS1      59.4    61   338  4     4.05  2.39
# … with 53,930 more rows

In the comments under the question, there was a case given when data(dataset) is strictly needed: when a package does not use the option LazyData: Yes.

If it is yes, the dataset is already visible and will be automatically loaded when first accessed. Otherwise it must be loaded with data(). "Lazy loading" was introduced to avoid unnecessary memory consumption of datasets coming with packages, while at the same time removing the need to use data().

Related