Skip to content

adding logical vector to data frame

2 messages · Emmanuel Paradis, Thomas J Vogels

#
At 15:31 07/09/01 -0400, Thomas Vogels <tov at ece.cmu.edu> wrote:
Hi,

I think that if you use names for indexing, you have to use single brackets
["..."], not double as in your example:
`data.frame':   2 obs. of  2 variables:
 $ x: int  1 2
 $ y: logi   TRUE FALSE


You'll need a more expert opinion than mine to explain the differences
between [ and [[, but look at the following:
A B C D E F G H I 
1 2 3 4 5 6 7 8 9
C 
3
[1] 3
[1] "C"
NULL

The same with numeric indexing, but not logical indexing:
C 
3
[1] 3
C 
3
Error: attempt to select more than one element



Emmanuel Paradis
Laboratoire de Pal?ontologie
Institut des Sciences de l'?volution
Universit? Montpellier II
F-34095 Montpellier c?dex 05
France
   phone: +33  4 67 14 39 64
     fax: +33  4 67 14 36 10
  mailto:paradis at isem.univ-montp2.fr
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
2 days later
#
Prof Brian D Ripley <ripley at stats.ox.ac.uk> writes:
Prof. Ripley,

thanks for the answer--both parts (confirmation of different treatment
of '$' and news from R-devel) are very helpful.


Emmanuel Paradis <paradis at isem.univ-montp2.fr> writes:

[snip]
Quite obviously, I'm not an expert, but in my simple-minded view, []
subsets, while [[]] extracts (one element, and one element only).
So x["C"] is the third row of x while x[["C"]] gives you the
third element (a scalar, no name).
because 'x == 3' results in a vector of the same length as x.  Since
that length is > 1, an error occurs.  (You could argue that if the
index vector contains exactly one TRUE value, '[['() could proceed,
but that's probably more dangerous than helpful.)

  -tom