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

jim holtman jholtman at gmail.com
Sun Aug 28 16:38:26 CEST 2011


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 <engin.yildiztepe at gmail.com> 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 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list