[R] Asking Favor For "Remove element with Particular Value In Vector"

William Dunlap wdunlap at tibco.com
Mon Aug 29 18:38:49 CEST 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bert Gunter
> Sent: Monday, August 29, 2011 7:07 AM
> To: Jim Lemon
> Cc: r-help at r-project.org
> Subject: Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
> 
> Jim et. al:
> 
> This is the second time I've seen this "advice" recently. Use logical
> indexing: which(), though not wrong, is superfluous:

which() will give the wrong answer if x does not
contain any elements of the set which you want to omit.
E.g.,
  > x <- 1:3
  > x[-which(x %in% c(0,255))] # bad
  integer(0)
  > x[!is.element(x, c(0,255))] # good
  [1] 1 2 3

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> 
> x[ !x %in% c(0,255)]  will do, rather than:
> 
> > If you want to remove the specific values 0 and 255 from your vector, try:
> >
> > x<-x[-which(x %in% c(0,255))]
> >
> > Jim
> >
> 
> -- Bert
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> 
> 
> --
> "Men by nature long to get on to the ultimate truths, and will often
> be impatient with elementary studies or fight shy of them. If it were
> possible to reach the ultimate truths without the elementary studies
> usually prefixed to them, these would not be preparatory studies but
> superfluous diversions."
> 
> -- Maimonides (1135-1204)
> 
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list