[Rd] undocumented name mangling during unlist?

Seth Falcon sfalcon at fhcrc.org
Tue Jan 9 18:43:51 CET 2007


Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:

> On Mon, 8 Jan 2007, Seth Falcon wrote:
>
>> The documentation for unlist says:
>>
>>     By default, 'unlist' tries to retain the naming information
>>     present in 'x'.  If 'use.names = FALSE' all naming information is
>>     dropped.
>>
>> Hence, I find it odd that I see the following:
>>
>>    > b <- list("highway1"=1:5)
>>    > unlist(b)
>>    highway11 highway12 highway13 highway14 highway15
>>            1         2         3         4         5
>>
>> I suspect this is essential behavior for something or other, but it
>> seems that this approach is inefficient for large lists and in many
>> contexts does not preserve names, but invents new ones.
>
> There is the information that the entries came from element "highway1"
> which needs to be preserved.  

In my view, preserving is not the same as creating a new name where
the original is a substring.

> c() and unlist() share code, and both try to create different names
> for elements of different origin.

I guess I don't understand.  In my example, aren't the elements all of
the same origin (from the highway1 entry of the list)?  

Granting a different interpretation of origin, it seems the attempt is
not very complete:

    v2 <- list(a=rep(1, 2), b=rep(2, 2), a=rep(3, 2))
    unlist(v2)  ## names are _not_ unique
    a1 a2 b1 b2 a1 a2 
     1  1  2  2  3  3 

> Why is this 'inefficient' (names are optional) and what do you
> propose that meets the conditions?

Could the origin name simply be repeated?

  unlist(b)
  highway1 highway1 highway1 highway1 highway1
         1        2        3        4        5

Efficiency could be gained by reusing the CHARSXP to name all elements
instead of having to created new strings for each name.

+ seth



More information about the R-devel mailing list