提取时接近preserving对象的属性/替换操作 [英] Approaches to preserving object's attributes during extract/replace operations

查看:164
本文介绍了提取时接近preserving对象的属性/替换操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在R $ C $ç遇到了以下问题。在一个函数,接受一个数据帧作为一个参数,我需要根据数据帧的原始列的值与计算出的数据添加(或更换,如果存在的话)的列。我写了code,但测试显示,数据帧的提取/替换操作,我已经使用,导致该对象的特殊(用户自定义)属性的损失

意识到,并通过读取R文件(的 http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.html ),我决定非常简单地解决问题 - 通过的节能的提取物之前的属性/替换操作和恢复的他们此后

  myTransformationFunction<  - 功能(数据){  #保存对象的属性
  ATTRS< - 属性(数据)  <数据帧的转换;涉及的`data`&GT提取/替换操作;  #恢复属性
  属性(数据)< - ATTRS  返回(数据)
}

这方法奏效。然而,一不小心,我遇到另一块R文件的跑了(的http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.data.frame.html),它提供恕我直言一个有趣的(和潜在的,更通用的?)的另一种方法以解决同样的问题:

  ##保持特殊属性:使用类与
##as.data.frame和[的方法:as.data.frame.avector< - as.data.frame.vector`[.avector`< - 功能(X,I,...){
  为r - NextMethod([)
  mostattributes(R)LT; - 属性(X)
   - [R
}D&下; - data.frame(ⅰ= 0:7中,f = GL(2,4),
                U =结构(11:18,单位=公斤的class =avector))
STR(D [2:4,-1])#'U'保持它的单位

我真的AP preciate如果这里的人们可以通过帮助:


  1. 比较上述的两种方法,如果他们有可比性(我认识到,所定义的第二种方法是为数据帧,但我怀疑它可以推广到任何物体)


  2. 解释中函数定义的语法和含义在第二个方法,尤其是 as.data.frame.avector ,如还有什么是行 as.data.frame.avector&LT的目的; - as.data.frame.vector



解决方案

我回答我的问题,因为我刚刚发现了一个SO问题(<一个href=\"http://stackoverflow.com/questions/10404224/how-to-delete-a-row-from-a-data-frame-without-losing-the-attributes\">How删除从data.frame行不失属性的),答案覆盖上面的我提出的问题。然而,更多的解释(的R初学者的)的第二种方法仍然是AP preciated。

更新:

另一种解决这个问题已经在答案提出以下的SO问题:索引操作删除属性。就个人而言,不过,我更喜欢这种方法的基础上,创建一个新的类,它的语义恕我直言清洁。

Recently I encountered the following problem in my R code. In a function, accepting a data frame as an argument, I needed to add (or replace, if it exists) a column with data calculated based on values of the data frame's original column. I wrote the code, but the testing revealed that data frame extract/replace operations, which I've used, resulted in a loss of the object's special (user-defined) attributes.

After realizing that and confirming that behavior by reading R documentation (http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.html), I decided to solve the problem very simply - by saving the attributes before the extract/replace operations and restoring them thereafter:

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)
}

This approach worked. However, accidentally, I ran across another piece of R documentation (http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.data.frame.html), which offers IMHO an interesting (and, potentially, a more generic?) alternative approach to solving the same problem:

## 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"

I would really appreciate if people here could help by:

  1. Comparing the two above-mentioned approaches, if they are comparable (I realize that the second approach as defined is for data frames, but I suspect it can be generalized to any object);

  2. Explaining the syntax and meaning in the function definition in the second approach, especially as.data.frame.avector, as well as what is the purpose of the line as.data.frame.avector <- as.data.frame.vector.

解决方案

I'm answering my own question, since I have just found an SO question (How to delete a row from a data.frame without losing the attributes), answers to which cover most of my questions posed above. However, additional explanations (for R beginners) for the second approach would still be appreciated.

UPDATE:

Another solution to this problem has been proposed in an answer to the following SO question: indexing operation removes attributes. Personally, however, I better like the approach, based on creating a new class, as it's IMHO semantically cleaner.

这篇关于提取时接近preserving对象的属性/替换操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆