From p.dalgaard at biostat.ku.dk  Thu Jul 12 10:55:45 2007
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Thu, 12 Jul 2007 10:55:45 +0200
Subject: [Rd] [[.data frame and row names
In-Reply-To: <Pine.LNX.4.64.0707120518480.15630@gannet.stats.ox.ac.uk>
References: <4695923B.8010405@fhcrc.org>
	<Pine.LNX.4.64.0707120518480.15630@gannet.stats.ox.ac.uk>
Message-ID: <4695EC91.9080208@biostat.ku.dk>

Prof Brian Ripley wrote:
> On Wed, 11 Jul 2007, Herve Pages wrote:
>
>   
>> Hi,
>>
>> I'm wondering why indexing a data frame by row name doesn't work
>> with [[. It works with [:
>>
>>  > sw <- swiss[1:5,1:2]
>>  > sw["Moutier", "Agriculture"]
>>  [1] 36.5
>>
>> but not with [[:
>>
>>  > sw[["Moutier", "Agriculture"]]
>>  Error in .subset2(.subset2(x, ..2), ..1) : subscript out of bounds
>>
>> The problem is really with the row name (and not the col name) since
>> this works:
>>
>>  > sw[[4, "Agriculture"]]
>>  [1] 36.5
>>
>> but not this:
>>
>>  > sw[["Moutier", 2]]
>>  Error in .subset2(.subset2(x, ..2), ..1) : subscript out of bounds
>>     
>
> .subset2 drops all attributes, including names.
>
>   
>> .subset2(sw, "Agriculture")
>>     
> [1] 17.0 45.1 39.7 36.5 43.5
>
> This is nothing new: R 2.0.0 did the same, for example.
>   
>
That is not quite the issue. The names aren't there to begin with. So 
more precisely, .subset2 does not *add* row.names(sw) as the names of 
its result. That, and the fact that sw[[r,c]] is defined to be sw[[c]][[r]].

It is the data frame constructors that drop names on the individual 
components. Notice that names are getting lost in constructions like this:

 > a <- data.frame(b=c(d=1,e=2),f=c(g=3,h=4))
 > dput(a)
structure(list(b = c(1, 2), f = c(3, 4)), .Names = c("b", "f"), 
row.names = c("d", "e"), class = "data.frame")

Whereas
 > a <- list(b=c(d=1,e=2))
 > .subset2(a,1)
d e
1 2



[Rd] [[.data frame and row names
Peter Dalgaard