From hpages at fhcrc.org  Thu Jul 12 04:30:19 2007
From: hpages at fhcrc.org (Herve Pages)
Date: Wed, 11 Jul 2007 19:30:19 -0700
Subject: [Rd] [[.data frame and row names
Message-ID: <4695923B.8010405@fhcrc.org>

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

No such problems with a matrix where everything works as expected:

  > msw <- as.matrix(sw)
  > msw[["Moutier", "Agriculture"]]
  [1] 36.5

Thanks!

H.



[Rd] [[.data frame and row names
Herve Pages