> -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Bert Gunter > Sent: Monday, August 29, 2011 7:07 AM > To: Jim Lemon > Cc: r-help@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@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@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. ______________________________________________ R-help@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.