[R] Odd behaviour of removing 'nothing' from an array or data frame

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Oct 31 16:49:45 CET 2006


Richard.Cotton at hsl.gov.uk writes:

> Thanks for the reply Peter, though I'm not quite convinced.
> 
> > > #dubious.records = integer(0)
> > > identical(dubious.records, -dubious.records)
> > [1] TRUE
> 
> > how can peoples.heights[-dubious.records,] be different from
> > peoples.heights[dubious.records,]? 
> 
> Tell me if I'm being willfully ignorant here, but I'm sure they should be 
> different.  In the first case, the minus sign represents subtraction, so 
> it is correct that dubious.records and -dubious.records are identical.
> 
> However, in the second case, inside the square brackets, the minus sign 
> represents set complement, not subtraction, so dubious.records and -
> dubious.records are not the same.

Nono, that is the point: Minus is minus, no matter where it occurs.
Changing what it means depending on context leads to inconsistent
behaviour. E.g., you shouldn't get different behaviour from assigning
an index expression to an intermediate variable, as in

  peoples.heights[-dubious.records,]
  keep <- -dubious.records
  peoples.heights[keep,]

However, that would happen if the minus was interpreted differently
within []: There is no way for R to realize that the sign change in
the assignment has anything to do with indexing (OK, there is, but
then the value of "keep" later on would depend on what it had been
used for! Things quickly get really nasty).



-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list