0 votes
1 view
in R Programming by (5.3k points)

I have a vector like a = c(1:10) and I need to remove multiple values, like 2, 3, 5

How to delete those numbers (they are NOT the positions in the vector) in the vector?

at the moment I loop the vector and do something like:

a[!a=NUMBER_TO_REMOVE]

But I think there is a function that does it automatically.

1 Answer

0 votes
by (25.3k points)

To delete multiple values, you can use the setdiff function from the base package as follows:

a <- c(1:10)

a

 [1]  1  2  3  4  5  6  7  8  9 10

remove <- c(2, 3, 5)

setdiff(a, remove)

[1]  1  4  6  7  8  9 10

You can also use the %in% operator as follows:

a <- c(1 : 10)

remove <- c (2, 3, 5)

a

 [1]  1  2  3  4  5  6  7  8  9 10

a %in% remove

 [1] FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE

a [! a %in% remove]

[1]  1  4  6  7  8  9 10

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...