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)
我想要添加到我的dataframe信息,就像使用?iris显示的数据一样(描述iris dataframe中的数据)
However I cannot find a way to do this.
但是我找不到这样做的方法。
3 个解决方案
#1
24
@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(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.
Hmisc包中的label()和units()提供了在data.frame中记录单个列的机制。contents(),然后在同一个包中总结您附加到data.frame的任何这些属性。
#2
20
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.
这些东西通常通过分割n个子设置来保存,但是有些进程会删除它们。这就是按价值传递的本质。
There may even be a package on CRAN for more complex metadata as attributes with some wrapper functions, but underneath its all attributes...
CRAN上甚至可能有一个包,用于将更复杂的元数据作为具有一些包装函数的属性,但是在它的所有属性之下……
#3
2
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
#1
24
@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(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.
Hmisc包中的label()和units()提供了在data.frame中记录单个列的机制。contents(),然后在同一个包中总结您附加到data.frame的任何这些属性。
#2
20
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.
这些东西通常通过分割n个子设置来保存,但是有些进程会删除它们。这就是按价值传递的本质。
There may even be a package on CRAN for more complex metadata as attributes with some wrapper functions, but underneath its all attributes...
CRAN上甚至可能有一个包,用于将更复杂的元数据作为具有一些包装函数的属性,但是在它的所有属性之下……
#3
2
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