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

时间:2011-10-27 17:04:08

标签: r dataframe

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

我想添加我的数据框信息,例如?iris显示的信息(描述虹膜数据帧中的数据)

但是我找不到办法做到这一点。

3 个答案:

答案 0 :(得分:25)

@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_.
来自label()包中的

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

答案 1 :(得分:21)

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

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

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

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

答案 2 :(得分:2)

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

请参阅?setRefClass,例如