如何快速检查内置数据集(PSA)?

6

使用内置数据集是制作可重现的问题的最佳方法之一。然而,使用data()非常令人沮丧,因为没有提供有关数据集结构的信息。

我如何快速查看可用数据集的结构?

1个回答

7
以下函数可能有所帮助:
dataStr <- function(fun=function(x) TRUE)
  str(
    Filter(
      fun,
      Filter(
        Negate(is.null),
        mget(data()$results[, "Item"], inh=T, ifn=list(NULL))
  ) ) )

它接受一个过滤函数,将其应用于所有数据集,并打印出匹配数据集的结构。例如,如果我们正在寻找矩阵:
> dataStr(is.matrix)
List of 8
 $ WorldPhones          : num [1:7, 1:7] 45939 60423 64721 68484 71799 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:7] "1951" "1956" "1957" "1958" ...
  .. ..$ : chr [1:7] "N.Amer" "Europe" "Asia" "S.Amer" ...
 $ occupationalStatus   : 'table' int [1:8, 1:8] 50 16 12 11 2 12 0 0 19 40 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ origin     : chr [1:8] "1" "2" "3" "4" ...
  .. ..$ destination: chr [1:8] "1" "2" "3" "4" ...
 $ volcano              : num [1:87, 1:61] 100 101 102 103 104 105 105 106 107 108 ...
--- 5 entries omitted ---

或者对于数据框(同样省略条目):
> dataStr(is.data.frame)
List of 42
 $ BOD             :'data.frame': 6 obs. of  2 variables:
  ..$ Time  : num [1:6] 1 2 3 4 5 7
  ..$ demand: num [1:6] 8.3 10.3 19 16 15.6 19.8
  ..- attr(*, "reference")= chr "A1.4, p. 270"
 $ CO2             :Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame':  84 obs. of  5 variables:
  ..$ Plant    : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ...
  ..$ Type     : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
  ..$ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ...
  ..$ conc     : num [1:84] 95 175 250 350 500 675 1000 95 175 250 ...
  ..$ uptake   : num [1:84] 16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ...
--- 40 entries omitted ---

甚至对于简单的向量:

> dataStr(function(x) is.atomic(x) && is.vector(x) && !is.ts(x))
List of 4
 $ euro   : Named num [1:11] 13.76 40.34 1.96 166.39 5.95 ...
  ..- attr(*, "names")= chr [1:11] "ATS" "BEF" "DEM" "ESP" ...
 $ islands: Named num [1:48] 11506 5500 16988 2968 16 ...
  ..- attr(*, "names")= chr [1:48] "Africa" "Antarctica" "Asia" "Australia" ...
 $ precip : Named num [1:70] 67 54.7 7 48.5 14 17.2 20.7 13 43.4 40.2 ...
  ..- attr(*, "names")= chr [1:70] "Mobile" "Juneau" "Phoenix" "Little Rock" ...
 $ rivers : num [1:141] 735 320 325 392 524 ...

非常有用。你的提问和回答很有趣,好像是为了后来的用户(比如我)创建一个资源。相关问题:如果一个OP输入的数据不是dput的输出,那么复制他们的数据并创建一个R对象的有效方法是什么,最有可能是尝试找出一个答案来提出。我在使用read.table(file = "clipboard")时遇到了困难。Tx - lawyeR
@lawyeR,这就是想法。我通常使用read.table(h=T, text="<paste>") - BrodieG
2
但我已经厌倦了这样做,所以我正在游说将使用内置数据集作为默认方式。 - BrodieG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接