From r@turner @end|ng |rom @uck|@nd@@c@nz  Mon Nov  4 21:28:00 2019
From: r@turner @end|ng |rom @uck|@nd@@c@nz (Rolf Turner)
Date: Tue, 5 Nov 2019 09:28:00 +1300
Subject: [Rd] Puzzled about a new method for "[".
In-Reply-To: <CABdHhvFZ2VL27iJb_hZmMK1Tmxq1gZKu7np1fNjk9F2KM2ME8Q@mail.gmail.com>
References: <74a6cf10-3266-fef8-9ea0-0ba6d36db807@auckland.ac.nz>
 <CABdHhvFZ2VL27iJb_hZmMK1Tmxq1gZKu7np1fNjk9F2KM2ME8Q@mail.gmail.com>
Message-ID: <3ccb53ef-a0a7-0449-28f2-fad550b719bf@auckland.ac.nz>


On 5/11/19 3:41 AM, Hadley Wickham wrote:

> For what it's worth, I don't think this strategy can work in general,
> because a class might have attributes that depend on its data/contents
> (e.g. https://vctrs.r-lib.org/articles/s3-vector.html#cached-sum). I
> don't think these are particularly common in practice, but it's
> dangerous to assume that you can restore a class simply by restoring
> its attributes after subsetting.


You're probably right that there are lurking perils in general, but I am 
not trying to "restore a class".  I simply want to *retain* attributes 
of columns in a data frame.

* I have a data frame X
* I attach attributes to certain of its columns;
      attr(X$melvin,"clyde") <- 42
   (I *don't* change the class of X$melvin.)
* I form a subset of X:
     Y <- X[1:100,3:10]
* given that "melvin" is amongst columns 3 through 10 of X,
     I want Y$melvin to retain the attribute "clyde", i.e. I
     want attr(Y$melvin,"clyde") to return 42

There is almost surely a better approach than the one that I've chosen
(isn't there always?) but it seems to work, and the perils certainly are
not immediately apparent to me.

cheers,

Rolf

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



[Rd] Puzzled about a new method for "[".
Rolf Turner