- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove a particular value from a vector in R?
To remove a particular value from a vector, we can use negation of value.
For example, if we have a vector called V that contains repetitive values starting from 1 to 10 and if we want to remove all 10s from V then we can use the command given below −
V<-V[V!=10]
Example 1
In order to remove 0 from vector x1, use the code given below −
x1<-rpois(100,2) x1
If you execute the above given code, it generates the following output −
[1] 1 1 1 1 3 1 1 2 3 4 2 2 1 2 2 5 2 1 4 1 2 1 2 3 1 3 1 1 4 4 3 3 4 2 0 1 1 [38] 2 1 0 0 2 5 1 1 3 1 1 2 1 1 4 1 4 0 1 0 2 2 1 2 2 1 4 2 2 2 5 4 2 0 0 1 8 [75] 1 2 4 1 2 0 0 1 0 0 2 4 1 3 1 1 3 1 3 0 2 3 1 2 8 4
Now, to remove 0 from vector x1, add the following code to the above code −
x1<-rpois(100,2) x1<-x1[x1!=0] x1
Output
If you execute all the above given codes as a single program, it generates the following output −
[1] 1 1 1 1 3 1 1 2 3 4 2 2 1 2 2 5 2 1 4 1 2 1 2 3 1 3 1 1 4 4 3 3 4 2 1 1 2 1 [39] 2 5 1 1 3 1 1 2 1 1 4 1 4 1 2 2 1 2 2 1 4 2 2 2 5 4 2 1 8 1 2 4 1 2 1 2 4 1 [77] 3 1 1 3 1 3 2 3 1 2 8 4
Example 2
Consider the code given below for vector x2 −
x2<-rpois(200,5) x2
If you execute the above given code, it generates the following output −
[1] 7 5 3 8 1 6 4 5 7 4 3 5 7 5 7 8 9 7 7 10 7 5 8 4 3 [26] 3 7 9 2 4 3 4 5 5 4 8 6 6 6 2 3 5 2 3 10 2 8 11 8 7 [51] 6 2 5 7 3 5 4 3 4 10 4 6 7 4 1 6 8 6 4 4 5 4 2 3 3 [76] 6 6 5 6 8 4 15 3 4 4 5 6 6 4 2 7 3 1 8 1 10 5 8 4 6 [101] 2 3 7 3 6 5 6 4 7 2 3 4 2 7 3 5 7 2 4 5 7 6 6 3 1 [126] 9 5 5 2 8 4 6 5 5 6 5 8 7 6 2 3 7 2 1 8 5 6 10 6 4 [151] 5 6 7 6 8 0 4 9 6 11 2 6 7 4 2 7 6 6 8 6 6 5 4 6 2 [176] 9 7 5 6 9 2 6 7 10 4 3 5 3 3 8 3 2 5 6 9 6 3 4 4 8
Now, to remove 5 from vector x2, add the following code to the above code −
x2<-x2[x2!=5] x2
Output
If you execute all the above given codes as a single program, it generates the following output −
[1] 7 3 8 1 6 4 7 4 3 7 7 8 9 7 7 10 7 8 4 3 3 7 9 2 4 [26] 3 4 4 8 6 6 6 2 3 2 3 10 2 8 11 8 7 6 2 7 3 4 3 4 10 [51] 4 6 7 4 1 6 8 6 4 4 4 2 3 3 6 6 6 8 4 15 3 4 4 6 6 [76] 4 2 7 3 1 8 1 10 8 4 6 2 3 7 3 6 6 4 7 2 3 4 2 7 3 [101] 7 2 4 7 6 6 3 1 9 2 8 4 6 6 8 7 6 2 3 7 2 1 8 6 10 [126] 6 4 6 7 6 8 0 4 9 6 11 2 6 7 4 2 7 6 6 8 6 6 4 6 2 [151] 9 7 6 9 2 6 7 10 4 3 3 3 8 3 2 6 9 6 3 4 4 8
Example 3
Consider the code given below for vector x3 −
x3<-sample(0:9,200,replace=TRUE) x3
If you execute the above given code, it generates the following output −
[1] 4 9 7 6 3 9 8 6 2 8 0 2 7 1 3 0 8 6 2 3 4 9 9 0 5 2 4 6 3 0 3 4 3 1 3 0 3 [38] 5 9 5 8 8 3 9 8 4 5 3 5 2 3 1 4 0 8 7 3 1 8 9 5 6 4 2 5 3 1 3 1 0 9 8 5 1 [75] 8 4 4 1 5 6 8 1 5 7 0 4 3 7 0 1 4 5 4 5 2 0 5 8 8 3 0 4 0 2 0 5 6 9 8 8 6 [112] 0 2 6 4 7 7 6 1 9 7 7 2 6 3 0 5 3 5 6 0 1 6 9 2 9 5 2 9 4 5 3 5 5 6 5 1 7 [149] 6 3 4 8 2 0 0 2 7 0 1 4 1 1 6 6 2 7 2 3 7 7 9 7 6 7 3 8 1 2 2 9 8 7 8 9 2 [186] 1 8 7 6 4 0 5 2 9 3 4 2 3 7 9
Now, to remove 4 from vector x3, add the following code to the above code −
x3<-sample(0:9,200,replace=TRUE) x3<-x3[x3!=4] x3
Output
If you execute all the above given codes as a single program, it generates the following output −
[1] 9 7 6 3 9 8 6 2 8 0 2 7 1 3 0 8 6 2 3 9 9 0 5 2 6 3 0 3 3 1 3 0 3 5 9 5 8 [38] 8 3 9 8 5 3 5 2 3 1 0 8 7 3 1 8 9 5 6 2 5 3 1 3 1 0 9 8 5 1 8 1 5 6 8 1 5 [75] 7 0 3 7 0 1 5 5 2 0 5 8 8 3 0 0 2 0 5 6 9 8 8 6 0 2 6 7 7 6 1 9 7 7 2 6 3 [112] 0 5 3 5 6 0 1 6 9 2 9 5 2 9 5 3 5 5 6 5 1 7 6 3 8 2 0 0 2 7 0 1 1 1 6 6 2 [149] 7 2 3 7 7 9 7 6 7 3 8 1 2 2 9 8 7 8 9 2 1 8 7 6 0 5 2 9 3 2 3 7 9
Example 4
Consider the code given below for vector x4 −
x4<-sample(1:10,200,replace=TRUE) x4
If you execute the above given code, it generates the following output −
[1] 10 2 5 6 8 10 1 5 8 4 5 6 5 8 5 10 1 1 3 6 7 9 8 7 9 [26] 1 9 4 6 10 3 1 6 6 9 8 5 1 2 8 2 8 3 5 1 9 7 2 2 10 [51] 3 10 2 6 9 4 10 2 4 4 10 8 3 3 4 10 3 4 7 1 10 10 10 10 7 [76] 6 1 3 4 4 9 9 7 7 2 9 7 2 6 2 9 4 10 5 5 2 3 10 4 5 [101] 6 3 7 4 5 9 3 10 6 2 2 2 9 3 3 9 5 8 9 10 1 2 7 6 8 [126] 10 4 10 4 2 9 2 10 8 9 6 4 7 8 2 9 7 3 3 7 5 10 10 2 1 [151] 7 2 10 6 6 9 7 10 6 5 2 7 10 4 9 3 7 4 7 2 8 5 1 1 10 [176] 8 9 9 2 4 2 9 9 10 7 4 1 5 7 9 9 2 10 8 1 1 8 5 9 10
Now, to remove 1 from vector x4, add the following code to the above code −
x4<-sample(1:10,200,replace=TRUE) x4<-x4[x4!=1] x4
Output
If you execute all the above given codes as a single program, it generates the following output −
[1] 10 2 5 6 8 10 5 8 4 5 6 5 8 5 10 3 6 7 9 8 7 9 9 4 6 [26] 10 3 6 6 9 8 5 2 8 2 8 3 5 9 7 2 2 10 3 10 2 6 9 4 10 [51] 2 4 4 10 8 3 3 4 10 3 4 7 10 10 10 10 7 6 3 4 4 9 9 7 7 [76] 2 9 7 2 6 2 9 4 10 5 5 2 3 10 4 5 6 3 7 4 5 9 3 10 6 [101] 2 2 2 9 3 3 9 5 8 9 10 2 7 6 8 10 4 10 4 2 9 2 10 8 9 [126] 6 4 7 8 2 9 7 3 3 7 5 10 10 2 7 2 10 6 6 9 7 10 6 5 2 [151] 7 10 4 9 3 7 4 7 2 8 5 10 8 9 9 2 4 2 9 9 10 7 4 5 7 [176] 9 9 2 10 8 8 5 9 10
Example 5
Consider the code given below for vector x5 −
x5<-rpois(200,1) x5
If you execute the above given code, it generates the following output −
[1] 2 1 1 1 4 1 0 0 1 2 0 0 1 2 1 0 1 1 3 0 0 3 0 2 3 0 1 0 1 2 1 0 0 2 0 0 0 [38] 1 3 2 1 0 0 1 0 4 1 0 0 3 2 1 1 1 1 2 2 1 2 0 0 0 0 1 1 1 0 0 3 0 2 1 0 0 [75] 0 1 1 2 3 1 0 3 0 0 0 0 0 3 0 3 0 0 0 0 2 0 0 1 1 1 2 2 1 1 0 0 0 1 1 1 0 [112] 2 1 2 2 1 2 1 0 0 3 0 1 2 3 0 1 0 1 0 0 1 0 1 1 1 2 0 2 1 1 0 2 1 1 2 0 2 [149] 0 0 0 3 3 3 2 0 0 2 0 1 1 1 0 3 0 1 0 1 0 3 1 1 0 2 4 2 0 0 1 2 0 0 0 1 1 [186] 0 0 1 1 2 0 0 3 1 0 0 2 1 1 1
Now to remove 0 from vector x5, add the following code to the above code −
x5<-rpois(200,1) x5<-x5[x5!=0] x5
Output
If you execute all the above given codes as a single program, it generates the following output −
[1] 2 1 1 1 4 1 1 2 1 2 1 1 1 3 3 2 3 1 1 2 1 2 1 3 2 1 1 4 1 3 2 1 1 1 1 2 2 [38] 1 2 1 1 1 3 2 1 1 1 2 3 1 3 3 3 2 1 1 1 2 2 1 1 1 1 1 2 1 2 2 1 2 1 3 1 2 [75] 3 1 1 1 1 1 1 2 2 1 1 2 1 1 2 2 3 3 3 2 2 1 1 1 3 1 1 3 1 1 2 4 2 1 2 1 1 [112] 1 1 2 3 1 2 1 1 1
- Related Articles
- How to remove names from a named vector in R?
- How to remove only last character from a string vector in R?
- How to remove rows from data frame in R based on grouping value of a particular column?
- How to remove all text from a string before a particular character in R?
- How to remove an item from a C++ STL vector with a certain value?
- How to remove a particular character from a String.
- How to create a vector with all dates in a particular year in R?
- How to replace a value in an R vector?
- How to remove some last elements of a vector in R?
- How to extract strings that contains a particular substring in an R vector?
- How to find the count of a particular character in a string vector in R?
- How to remove rows containing missing value based on a particular column in an R data frame?
- How to add a value to a particular matrix element in R?
- How to replace a particular value in R data frame with a new value?
- How to extract the maximum value from named vector in R?
