Get a list of the data sets in a particular package

前端 未结 3 1141
孤城傲影
孤城傲影 2020-12-01 07:37

I would like to get a list of all the data sets in a particular R package shown in the console. I know that the function data() will list all the data sets in l

相关标签:
3条回答
  • 2020-12-01 07:49

    There's some good info on this in the details section of help(data). Here are the basics, using the plyr package as an example. For starters, let's see what's available from data().

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

    Further investigation of those elements will reveal what's in them. Next, we can use the arguments in data() and then subset the resulting list to find the names of the data sets in the package.

    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
    

    Edit/Update: If you wish to find the names of data sets in all installed packages, you can use the following. .packages(TRUE) gives all packages available in the library location path lib.loc. Since the data sets in the base and stats packages have been moved to the datasets package, we need to account for that by taking them out with 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"   
    
    0 讨论(0)
  • 2020-12-01 07:54

    If you are in the R-studio and you have imported that package

    you can switch from global environment to the specific package in your "environment" window

    Then you can see the list of the data set in that package

    0 讨论(0)
  • 2020-12-01 08:14

    The vcdExtra package has a function datasets for just this purpose. It returns a data frame containing the name, class, dimensions, and title of each data set found in a package.

    > 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.
    >
    

    It works with several package names also:

    > 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
    >
    
    0 讨论(0)
提交回复
热议问题