Skip to content
Prev 171615 / 398503 Next

learning R

David Winsemius 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

vQ