如何向 R 中的 data.frame 添加文档?

发布于 2024-12-12 01:14:37 字数 170 浏览 0 评论 0原文

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

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

但是我找不到一种方法来做到这一点。

I've been using R for a while and I've realized it would help a lot if you could attach a description data contained in the data.frame, because you could gather all useful research information in a .Rdata file.

I want to add to my dataframe info like the one is displayed by ?iris (describing the data in the iris dataframe)

However I cannot find a way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑忘罢 2024-12-19 01:14:37

@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 的所有这些属性。

@Spacedman has the good general answer for this sort of thing.

If you'd like something a little fancier, you could try out 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_.

label() and units() from the in the Hmisc package provide mechanisms for documenting individual columns in data.frames. contents(), in the same package then summarizes any of these attributes you've attached to the data.frame.

撩动你心 2024-12-19 01:14:37

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

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

这些内容大部分是通过切片 n 子集来保留的,但某些进程会删除它们。这就是价值传递系统的本质。

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

You can add it as an arbitrary attribute:

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

These things are mostly preserved by slicing n subsetting, but some processes will drop them. Such is the nature of a pass-by-value system.

There may even be a package on CRAN for more complex metadata as attributes with some wrapper functions, but underneath its all attributes...

无边思念无边月 2024-12-19 01:14:37

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

例如,请参见 ?setRefClass

Another possibility would be to turn your df into an object of a formal class (s4, reference class) with two fields - say "data" (your df) and "info" (character string with description)

See ?setRefClass, for example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文