最近我在R代码中遇到了以下问题。在一个接受数据帧作为参数的函数中,我需要添加(或替换,如果存在的话)一个列,其中的数据是根据数据帧的原始列的值计算出来的。我编写了代码,但是测试显示,我使用的数据帧提取/替换操作导致丢失了对象的特殊(用户定义的)属性。
在意识到这一点并通过阅读R文档(http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.html)确认该行为后,我决定非常简单地解决这个问题-在提取/替换操作之前保存属性,然后恢复它们:
myTransformationFunction <- function (data) {
# save object's attributes
attrs <- attributes(data)
<data frame transformations; involves extract/replace operations on `data`>
# restore the attributes
attributes(data) <- attrs
return (data)
}
这个方法有效。然而,我偶然发现了另一份R文档(http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.data.frame.html),它为IMHO提供了一种有趣的(可能是更通用的?)替代方法来解决相同的问题:
## keeping special attributes: use a class with a
## "as.data.frame" and "[" method:
as.data.frame.avector <- as.data.frame.vector
`[.avector` <- function(x,i,...) {
r <- NextMethod("[")
mostattributes(r) <- attributes(x)
r
}
d <- data.frame(i = 0:7, f = gl(2,4),
u = structure(11:18, unit = "kg", class = "avector"))
str(d[2:4, -1]) # 'u' keeps its "unit"
如果这里的人能帮助我,我将非常感激:
比较上述两种方法,如果它们具有可比性(我意识到第二种方法是针对数据帧定义的,但我怀疑它可以推广到任何对象);
解释第二种方法中函数定义中的语法和含义,特别是
as.data.frame.avector
,以及as.data.frame.avector <- as.data.frame.vector
行的目的。
我正在回答我自己的问题,因为我刚刚发现了一个SO问题(如何从data.frame中删除一行而不丢失属性),其中的答案涵盖了大多数我上面提出的问题。但是,对于第二种方法的额外解释(对于R初学者)仍然值得赞赏。更新:
这个问题的另一个解决方案在以下SO问题的回答中被提出:索引操作删除属性。然而,就我个人而言,我更喜欢这种基于创建新类的方法,因为我认为它在语义上更干净。