[Rd] [R] learning R
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Feb 25 10:32:03 CET 2009
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
>
More information about the R-devel
mailing list