【问题标题】:How to add documentation to a data.frame in R?如何将文档添加到 R 中的 data.frame?
【发布时间】:2011-12-16 16:54:20
【问题描述】:

我使用 R 已经有一段时间了,我意识到如果您可以附加包含在 data.frame 中的描述数据,这将有很大帮助,因为您可以在 .Rdata 文件中收集所有有用的研究信息。

我想将信息添加到我的数据框中,就像 ?iris 显示的一样(描述 iris 数据框中的数据)

但是我找不到这样做的方法。

【问题讨论】:

标签: r dataframe


【解决方案1】:

@Spacedman 对这类事情有很好的通用答案。

如果你想要一些更高级的东西,你可以试试comment()

 comment(iris) <- 
 "     This famous (Fisher's or Anderson's) iris data set gives the
 measurements in centimeters of the variables sepal length and
 width and petal length and width, respectively, for 50 flowers
 from each of 3 species of iris.  The species are _Iris setosa_,
 _versicolor_, and _virginica_.\n"

 cat(comment(iris))
 # This famous (Fisher's or Anderson's) iris data set gives the
 # measurements in centimeters of the variables sepal length and
 # width and petal length and width, respectively, for 50 flowers
 # from each of 3 species of iris.  The species are _Iris setosa_,
 # _versicolor_, and _virginica_.

Hmisc 包中的label()units() 提供了记录data.frames 中各个列的机制。 contents(),然后在同一个包中总结了您附加到 data.frame 的任何这些属性。

【讨论】:

  • commentbase 包中,而不是Hmisc
  • @RichieCotton -- 感谢您指出这一点。我想假设comment 在附加Hmisc 之后总是可用 ...因此它是由Hmisc 提供 的,这是不公平的;)
【解决方案2】:

您可以将其添加为任意属性:

attr(df,"doc") = "This is my documentation"

这些东西大多是通过切片 n 子集来保留的,但有些进程会丢弃它们。这就是价值传递系统的本质。

CRAN 上甚至可能有一个包用于更复杂的元数据作为具有一些包装函数的属性,但在其所有属性之下...

【讨论】:

    【解决方案3】:

    另一种可能性是将您的df 转换为具有两个字段的正式类(s4,参考类)的对象 - 比如说“数据”(您的 df)和“信息”(带有描述的字符串)

    例如见?setRefClass

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 2016-08-25
      相关资源
      最近更新 更多