【问题标题】:Get a list of the data sets in a particular package获取特定包中的数据集列表
【发布时间】:2014-12-30 17:36:41
【问题描述】:

我想获取控制台中显示的特定 R 包中所有数据集的列表。我知道函数data() 将列出加载包中的所有数据集。那不是我的目标。我想获取特定 R 包中所有数据集的列表。以下尝试不起作用。

data()
data('arules')
# Warning message:
# In data("arules") : data set ‘arules’ not found

我的另一个目的是获取特定包中所有数据集的dim 列表。

【问题讨论】:

标签: r


【解决方案1】:

help(data) 的详细信息部分提供了一些很好的信息。以下是基础知识,以 plyr 包为例。首先,让我们看看 data() 提供的内容。

names(data())
#[1] "title"   "header"  "results" "footer" 

对这些元素的进一步调查将揭示其中的内容。接下来,我们可以使用data() 中的参数,然后对结果列表进行子集化,以查找包中数据集的名称。

d <- data(package = "plyr")
## names of data sets in the package
d$results[, "Item"]
# [1] "baseball" "ozone"   
## assign it to use later
nm <- d$results[, "Item"]
## call the promised data
data(list = nm, package = "plyr")
## get the dimensions of each data set
lapply(mget(nm), dim)
# $baseball
# [1] 21699    22
#
# $ozone
# [1] 24 24 72

编辑/更新:如果您希望在所有已安装的包中查找数据集的名称,您可以使用以下命令。 .packages(TRUE) 提供库位置路径lib.loc 中可用的所有包。由于 basestats 包中的数据集已移至 datasets 包中,因此我们需要将它们取出来setdiff()

## names of all packages sans base and stats
pkgs <- setdiff(.packages(TRUE), c("base", "stats"))
## get the names of all the data sets
dsets <- data(package = pkgs)$result[, "Item"]
## look at the first few in our result
head(dsets)
# [1] "AirPassengers"          "BJsales"                "BJsales.lead (BJsales)"
# [4] "BOD"                    "CO2"                    "ChickWeight"   

【讨论】:

    【解决方案2】:

    vcdExtra 包有一个函数datasets 就是为了这个目的。它返回一个数据框,其中包含在包中找到的每个数据集的名称、类、维度和标题。

    > vcdExtra::datasets("plyr")
          Item      class      dim                                                        Title
    1 baseball data.frame 21699x22 Yearly batting records for all major league baseball players
    2    ozone      array 24x24x72             Monthly ozone measurements over Central America.
    >
    

    它也适用于多个包名称:

    > vcdExtra::datasets(c("plyr", "dplyr"))
      Package     Item      class      dim
    1    plyr baseball data.frame 21699x22
    2    plyr    ozone      array 24x24x72
    3   dplyr     nasa   tbl_cube  41472x4
                                                             Title
    1 Yearly batting records for all major league baseball players
    2             Monthly ozone measurements over Central America.
    3                                    NASA spatio-temporal data
    >
    

    【讨论】:

      【解决方案3】:

      如果你在 R-studio 中并且你已经导入了那个包

      您可以在“环境”窗口中从全局环境切换到特定包

      然后就可以看到那个包里面的数据集列表了

      【讨论】:

      • 不给出数据集,只给出函数。
      • 有用!尽管如此。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 2015-05-02
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多