[Rd] [R] learning R

markleeds at verizon.net markleeds at verizon.net
Wed Feb 25 10:39:27 CET 2009


  Hi Wacek: Somewhere I remember reading that environments have 
functionality like lists EXCEPT for the names part. IIRC, I think that I 
read this in the R Language Reference manual also.



On Wed, Feb 25, 2009 at  4:32 AM, Wacek Kusnierczyk wrote:

> 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
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-devel mailing list