Skip to content

Accessing just the value of an object but not the attributes

8 messages · Johann Petrak, Martin Maechler, Murray Jorgensen +2 more

#
I have the feeling that this is a dumb question but oh well ...
When I have an object x with several attributes,
y <- x will make a copy of x with all the attributes included.
What if I just want y to get the value assigned but nothing
else (no class, names, dim or other attributes)?

Johann
#
Johann> I have the feeling that this is a dumb question but oh well ...
no, actually not at all dumb

    Johann> When I have an object x with several attributes, y <- x will
    Johann> make a copy of x with all the attributes included.  What if I
    Johann> just want y to get the value assigned but nothing else (no
    Johann> class, names, dim or other attributes)?

The answer -- `unfortunately' IMHO --- 
is  as.vector() :
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

"unfortunately" : 
		IMO, a vector with attributes is still a vector, particularly
		if these are just names.. hence as.vector() should leave it
		alone, but -- alas -- it's for compatibility reasons too
		important to be changed. 
So even if a new  dropattributes() function would do what as.vector() does
now, it would break too much code if as.vector() was changed...
Note that "unfortunately" is my personal opinion, not a general R core one.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
I see the role of as.vector().
But even if as.vector() needs to keep its attribute-dropping
side-effect for compatibility reasons, wouldn't
a dropattr() still be a good idea?
For instance, I can assign attributes to closures, but
wont get rid of them using as.vector() - and neither
using as.function() by the way ...
So if I dont know in advance what kind of object it is
and I do want to just access the "value" in any case
I have to check the type ... ?


Johann
Martin Maechler wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Johann> I see the role of as.vector().  But even if as.vector() needs
    Johann> to keep its attribute-dropping side-effect for compatibility
    Johann> reasons, wouldn't a dropattr() still be a good idea?  For
    Johann> instance, I can assign attributes to closures, but wont get rid
    Johann> of them using as.vector() - and neither using as.function() by
    Johann> the way ...  So if I dont know in advance what kind of object
    Johann> it is and I do want to just access the "value" in any case I
    Johann> have to check the type ... ?

Continuing another example from R-help, where

  > foo <- function(x) { y <- x; class(y) <- "foo"; y }

Let's try

  > attr(foo,"myattr") <- "my attribute"
  > foo
  function(x) { y <- x; class(y) <- "foo"; y }
  attr(,"myattr")
  [1] "my attribute"
  > as.vector(foo, mode="function")
  function (x) 
  {
      y <- x
      class(y) <- "foo"
      y
  }
  > 

So, that *does* work for functions, and you would think

    dropAttributes <- function(x) as.vector(x, mode = mode(x))

should work in most cases {but not for environments() or stranger things
like promisses ..}. 

The reason:  "vector" is used -- and confused --
in S (the language, R being one implementation)
in at least too fundamentally different ways :

  1) an atomic vector {numeric, character,..}

  2) a "generic" vector.  List()s and expression()s being generic vectors, too.

However, as a matter of fact, as.vector() doesn't drop attributes for list()s.
{not caring if this could be considered a bug or not; as you know, "me thinks"
 particularly about as.vector() ..}

There, and in general you could use

    dropAttributesDANGER <- function(x) {attributes(x) <- NULL ; x}

the "DANGER" lying in the fact that you *do* lose all attributes, including
    dim, class, names, dimnames, ...
which might entail that  dropAttributesDANGER(obj) might print tons of lines
since print.default() is used instead of  print.<class of obj>().

If, instead, you'd use proper OO techniques, you'd have a class "foobar" and
a `value()' (say) generic with a method for your class, i.e.
   value.foobar <- function(x) {attributes(x) <- NULL ; x}

and in your code, you'd use  ` value(myspecialobject) '

BTW: Yes,   function(x) {attributes(x) <- NULL ; x}
 could probably made memory-efficient (no copying) by making it
 .Internal(); however, I don't know if it's worth it.
Johann> Martin Maechler wrote:
>> [...]
    >> 
    >> "unfortunately" : IMO, a vector with attributes is still a vector,
    >> particularly if these are just names.. hence as.vector() should
    >> leave it alone, but -- alas -- it's for compatibility reasons too
    >> important to be changed.  So even if a new dropattributes() function
    >> would do what as.vector() does now, it would break too much code if
    >> as.vector() was changed...  Note that "unfortunately" is my personal
    >> opinion, not a general R core one.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
I've just upgraded to 1.3.1. The new installer SetupR.exe is wonderful!
Thanks,
Brian! Also install.packages() works well.

A few questions, though.

1. There seems to be no help on tseries.

2. The old packages from my earlier installation of R appear along with the
new
packages in the html packages help file. I find that I can load these packages
still, with a lot of searching noise coming from the disk drive. Now this may
be an advantage, but I will surely want to junk some of these old packages
soon. I wonder if things will get messy at this point?

3. Some familiar old packages are gone. I wonder where their functionality is
now? I'm thinking of (for me) ctest, eda, lqs, modreg, mva, splines, stepfun,
tcltk. (I won't ask this about ts - maybe I can use ts help for tseries
functions?)

Murray Jorgensen




Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html 
Department of Statistics, University of Waikato, Hamilton, New Zealand 
Email: maj at waikato.ac.nz                            Fax +64-7 838 4155
Phone +64-7 838 4773 home phone +64-7 856 6705  Mobile +64-21 139 5862

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Murray Jorgensen <maj at waikato.ac.nz> writes:
Um, that's not supposed to happen... you're supposed to end up with a
....\rw1031\library directory with all of those inside of it. Would
you happen to have an old setting for your R library confusing things???

tseries is not among the "recommended" packages that you get by
default, so you would normally get that from CRAN and that should put
the help files in place too.
#
I see my error now. Those packages I mention are in "base".
At 09:50 PM 3-10-01 +1200, Murray Jorgensen wrote:
packages
.-.-
._._
Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html 
Department of Statistics, University of Waikato, Hamilton, New Zealand 
Email: maj at waikato.ac.nz                            Fax +64-7 838 4155
Phone +64-7 838 4773 home phone +64-7 856 6705  Mobile +64-21 139 5862

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
2 days later
#
On Tue, 2 Oct 2001, Johann Petrak wrote:

            
Not needed, as

attributes(x) <- NULL

removes all attributes.  I have no idea why this might be useful to you
.. remember that class and dim are attributes, for example.

Because they are not normally visible, assignment functions like
attributes<- are too little appreciated in S programming.
[...]