Re: [Rd] [[.data frame and row names

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu, 12 Jul 2007 05:24:12 +0100 (BST)

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.

> No such problems with a matrix where everything works as expected:
>
> > msw <- as.matrix(sw)
> > msw[["Moutier", "Agriculture"]]
> [1] 36.5
>
> Thanks!
>
> H.

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 12 Jul 2007 - 04:29:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 12 Jul 2007 - 09:36:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.