From Waclaw.Marcin.Kusnierczyk at idi.ntnu.no  Wed Feb 25 10:32:03 2009
From: Waclaw.Marcin.Kusnierczyk at idi.ntnu.no (Wacek Kusnierczyk)
Date: Wed, 25 Feb 2009 10:32:03 +0100
Subject: [Rd] [R] learning R
In-Reply-To: <49A5046A.1010801@idi.ntnu.no>
References: <B011ECE5-387C-41D3-B710-C221D4BF9FB2@gmail.com>	<6E853AE7-8581-4EF1-B64A-A351AA96D79B@comcast.net>
	<49A5046A.1010801@idi.ntnu.no>
Message-ID: <49A51013.6020703@idi.ntnu.no>

a quick follow-up:

    e = new.env()
    e$a = 1
    names(e)
    # NULL
    names(e) = 'a'
    # error in names(e) = "foo" : names() applied to a non-vector

this is surprising.  names(e) 'works', there is no complaint, but when
names<- is used, the error is about the use of names, not names<-.

btw. ?names says:

"Description:

     Functions to get or set the names of an object.

Usage:

     names(x)
     names(x) <- value

Arguments:

       x: an R object.
"

and there is no clarification in the rest of the page that x cannot be
an environment, or that it has to be a vector.  furthermore:

    p = pairlist(a=1)
    names(p)
    # "a"
    names(p) = 'b'
    # fine
    is.vector(p)
    # FALSE

which is incoherent with the above error message, in that p is *not* a
vector.

vQ



Wacek Kusnierczyk wrote:
>
> the following:
>
>     names(a[2]) = 'foo'
>
> has (partially) a functional flavour, in that you assign to the names of
> a *copy* of a part of a, while
>
>     names(a)[2] = 'foo'
>
> does not have the flavour, in that you assign to the names of a;  it
> seems, according to the man page you quote, to be equivalent to:
>
>     a = 'names<-'(a, '[<-.'(names(a), 2, 'foo'))
>
> which proceeds as follows:
>
>     tmp1 = names(a)
>     # get a copy of the names of a, no effect on a
>
>     tmp2 = '[<-'(tmp1, 2, 'foo')
>     # get a copy of tmp1 with the second element replaced with 'foo'
>     # no effect on either a or tmp1
>
>     tmp3 = 'names<-'(a, tmp2)
>     # get a copy of a with its names replaced with tmp2
>     # no effect on either a, tmp1, or tmp2
>
>     a = tmp3
>     # backassign the result to a
>



[Rd] [R] learning R
Wacek Kusnierczyk