How to remove value from a vector in R

r

#1

I have created a vector and I want to delete some values from vector but I am not able to perform it
for example
x<-1:30
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
and I want that number from 3 to 10 should remove


#2

Declare a boolean vector that has TRUE at all the positions you want to retain and FALSE at those you want to delete. Suppose that vector is y.

Then, x[y] will give you the requires output.

For the above question y is the vector that contain FALSE from 3 to 10 and TRUE elsewhere.

Reply with the code if it doesn’t work. Like this if it does.


#3
x <- 1:30
x2 <- x[!x %in% 3:10]

#4

The easiest solution (in my opinion): x[-3:-10].

Btw: @Pierre_Lafortune: Your code deletes elements 3 AND 10, not 3 to 10. Correct is x2 <- x[!x %in% 3:10]

Hope this helps.


#5

Thanks for the edit. I think I can offer an edit also. Your code can be simplified to:

x[-(3:10)]

#6

First you need to ensure that all your output is saved
X=X[11:30]


#7

Suppose My X is this : > x
[1] 12 23 34 45 35 23 12

x[length(x)-4] = 49
x
[1] 12 23 49 45 35 23 12

means 34 I wanted to remove and replace with 49,


#8

To remove a value from a Vector permanently and Update that Vector meas:

x
[1] 12 23 49 45 35 23 12
x = x[x!= 23]
x
[1] 12 49 45 35 12
x