Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
But that is a good reason to always use parentheses: x[ !(x %in% c(0,255))] since some of the 'precendences' vary between languages. On Tue, Aug 30, 2011 at 4:47 AM, Jim Lemon wrote: > On 08/30/2011 12:06 AM, Bert Gunter wrote: >> >> Jim et. al: >> >> This is the second time I've seen this "advice" recently. Use logical >> indexing: which(), though not wrong, is superfluous: >> >> >> x[ !x %in% c(0,255)] will do, rather than: >> > By golly, you're right, and it works even if x is a logical vector. I should > have checked the operator precedence. > > Jim > > __ > 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. > -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
On 08/30/2011 12:06 AM, Bert Gunter wrote: Jim et. al: This is the second time I've seen this "advice" recently. Use logical indexing: which(), though not wrong, is superfluous: x[ !x %in% c(0,255)] will do, rather than: By golly, you're right, and it works even if x is a logical vector. I should have checked the operator precedence. Jim __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
> -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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
Thank you very much,friend. -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3776435.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
Thank you very much,friend. -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3776430.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
Thank you very much,friend. -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3776427.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
Thank you friend for suggestion. -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3776432.html Sent from the R help mailing list archive at Nabble.com. __ 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.
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: 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
chuan_zl wrote: > Dear All. > > I am Chuan. I am beginner for R.I facing some problem in remove element from > vector.I have a vector with size 238 element as follow(a part) > > [1] 0 18 24 33 44..[238] 255 > > Let the vector label as "x",I want remove element "0" and "255".I try use > such function: > > x[x>0 & x<255] Hi Chuan, If you want to remove the specific values 0 and 255 from your vector, try: x<-x[-which(x %in% c(0,255))] Jim __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
Be careful about negating the 'which' in case there are no matches: > x <- 1:10 > x[-which(x == 11)] integer(0) > Notice it deletes the whole vector. Safer to use logical vectors: > x[!(x==3 | x == 7)] [1] 1 2 4 5 6 8 9 10 > x[!(x == 11)] # notice this works [1] 1 2 3 4 5 6 7 8 9 10 > On Sun, Aug 28, 2011 at 7:20 AM, eyildiz wrote: > You can use 'which' and negative subscripts to remove elements from a vector. > > y<-x[-(which(x==0|x==255))] > > > > chuan_zl wrote: >> >> Dear All. >> >> I am Chuan. I am beginner for R.I facing some problem in remove element >> from vector.I have a vector with size 238 element as follow(a part) >> >> [1] 0 18 24 33 44..[238] 255 >> >> Let the vector label as "x",I want remove element "0" and "255".I try use >> such function: >> >> x[x>0 & x<255] >> >> However, I am fail since same results are give even try it for many >> times.I also try with shorter vector with 10 element. It is successfully >> resulted. So,want can I do for it. Kindly asking favor for expert here. >> Thank you very much. >> >> Chuan >> > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3774271.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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. > -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
You can use 'which' and negative subscripts to remove elements from a vector. y<-x[-(which(x==0|x==255))] chuan_zl wrote: > > Dear All. > > I am Chuan. I am beginner for R.I facing some problem in remove element > from vector.I have a vector with size 238 element as follow(a part) > > [1] 0 18 24 33 44..[238] 255 > > Let the vector label as "x",I want remove element "0" and "255".I try use > such function: > > x[x>0 & x<255] > > However, I am fail since same results are give even try it for many > times.I also try with shorter vector with 10 element. It is successfully > resulted. So,want can I do for it. Kindly asking favor for expert here. > Thank you very much. > > Chuan > -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3774271.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
On Aug 27, 2011, at 5:31 AM, chuan_zl wrote: Dear All. I am Chuan. I am beginner for R.I facing some problem in remove element from vector.I have a vector with size 238 element as follow(a part) [1] 0 18 24 33 44..[238] 255 Let the vector label as "x",I want remove element "0" and "255".I try use such function: x[x>0 & x<255] I am not completely clear but it appears that you want to remove the first and last elements. You can use negative indexing vectors. x[ -c(1, length(x) ) ] However, I am fail Perhaps your vector is a factor? Try this and see what you get: str(x) since same results are give even try it for many times.I also try with shorter vector with 10 element. It is successfully resulted. So,want can I do for it. Kindly asking favor for expert here. Thank you very much. Chuan -- David Winsemius, MD West Hartford, CT __ 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.
Re: [R] Asking Favor For "Remove element with Particular Value In Vector"
Not sure whether I understand your question right but here is what I would do: # Sample data x <- seq( 1, 100, by=6) x [1] 1 7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97 # remove element with value 19 x <- x[ x != 19 ] x [1] 1 7 13 25 31 37 43 49 55 61 67 73 79 85 91 97 If you want to remove values smaller / larger than a certain threshold, your way should work well: # Sample data x <- seq( 1, 100, by=6) x[9] <- 155 x [1] 1 7 13 19 25 31 37 43 155 55 61 67 73 79 85 91 97 # Remove elements smaller than 20 or larger than 80: x <- x[ x > 20 & x < 80 ] x [1] 25 31 37 43 55 61 67 73 79 So there is probably an issue with your data vector - why don't you dput() it? Rgds, Rainer On Saturday 27 August 2011 02:31:29 chuan_zl wrote: > Dear All. > > I am Chuan. I am beginner for R.I facing some problem in remove element from > vector.I have a vector with size 238 element as follow(a part) > > [1] 0 18 24 33 44..[238] 255 > > Let the vector label as "x",I want remove element "0" and "255".I try use > such function: > > x[x>0 & x<255] > > However, I am fail since same results are give even try it for many times.I > also try with shorter vector with 10 element. It is successfully resulted. > So,want can I do for it. Kindly asking favor for expert here. Thank you very > much. > > Chuan > > -- > View this message in context: > http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particul > ar-Value-In-Vector-tp3772779p3772779.html Sent from the R help mailing list > archive at Nabble.com. > > __ > 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.