Skip to content
Prev 300404 / 398503 Next

subsetting without losing the attributes

Inline
On 2012-07-17 04:16, MK wrote:
You don't. At least not without modifying subset.data.frame().

The key sentence on the ?Extract help page is this:

  "Subsetting (except by an empty index) will drop all
   attributes except names, dim and dimnames."

(This should probably say "... by an empty _first_ index ...")

If you really want a subset function to do what you want then
it's an easy modification; just replace the last line of
subset.data.frame with

      if(isTRUE(r))
          x[, vars, drop = drop]
      else
          x[r, vars, drop = drop]

But this still won't let you pick subsets of rows without
losing the attributes. So I see no advantage over [,].

I would just save the attributes and reassign them.

Peter Ehlers