简体   繁体   English

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

[英]How to add documentation to a data.frame in R?

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

I want to add to my dataframe info like the one is displayed by ?iris (describing the data in the iris dataframe) 我想添加我的数据帧信息,例如?iris显示的信息(描述虹膜数据帧中的数据)

However I cannot find a way to do this. 但是我找不到办法做到这一点。

@Spacedman has the good general answer for this sort of thing. @Spacedman对这类事情有一个很好的一般答案。

If you'd like something a little fancier, you could try out comment() . 如果你想要一些有点发烧友的东西,你可以试试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. Hmisc包中的label()units()提供了记录data.frames中各个列的机制。 contents() , in the same package then summarizes any of these attributes you've attached to the data.frame. contents() ,在同一个包中,然后汇总您附加到data.frame的任何这些属性。

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. 这些东西大多是通过切片n子集来保留的,但是有些进程会删除它们。 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... CRAN上甚至可能有一个包用于更复杂的元数据作为包含某些包装函数的属性,但在其所有属性下面...

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) 另一种可能性是将你的df转换为具有两个字段的正式类(s4,引用类)的对象 - 比如“data”(你的df)和“info”(带描述的字符串)

See ?setRefClass , for example 例如,参见?setRefClass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM