[R] delete data row

William Dunlap wdunlap at tibco.com
Mon Oct 18 00:56:27 CEST 2010


> I had been thinking of:
>  > x <- c(1, (2^(0.5))^2 , 3, 5, (2^(0.5))^2 , 3, 1)
>  > y <- 2
>  > x[-which(zapsmall(x-y) == 0)]
> [1] 1 3 5 3 1

Using which() to convert logicals into integer
subscripts is almost always unnecessary and often wrong.
In this case it fails when no x is close to y,
because integer(0) is the same thing as -integer(0):

  > x[-which(zapsmall(x-10) == 0)]
  numeric(0)

The whichless version, using logical subscripts,
works (in this case we want all of x):

  > x[zapsmall(x-10)!=0]
  [1] 1 2 3 5 2 3 1

When using logicals as subscripts, read the "["
as "such that".

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 



More information about the R-help mailing list