- 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 the first replicate in a vector using another vector that contains similar elements in R?
To remove the first replicate in a vector using another vector, we can use match function. For example, if we have a vector x that contain values 0, 1, 2, 3, 4, 5 and 0, 1, 2 are repeated in another vector say then the removal of this replicate will result in values 3, 4, and 5. This can be done by using x[-match(y,x)].
Example
x1<-0:5 x1
Output
[1] 0 1 2 3 4 5
Example
y1<-0:2 y1
Output
[1] 0 1 2
Example
x1[-match(y1,x1)]
Output
[1] 3 4 5
Example
x2<-sample(0:5,120,replace=TRUE) x2
Output
[1] 0 3 3 4 3 0 4 2 1 3 0 2 0 1 3 1 5 2 3 1 1 2 5 2 0 0 3 1 3 0 3 0 0 0 3 2 4 [38] 0 0 2 5 5 0 1 3 1 2 5 5 2 2 4 1 5 3 3 3 4 1 4 0 1 4 1 3 3 4 0 1 1 0 3 1 4 [75] 5 2 5 1 3 4 3 3 1 3 3 2 1 2 3 4 0 1 1 5 4 0 4 1 3 0 0 0 2 1 3 2 4 5 2 5 3 [112] 5 0 4 5 4 1 0 0 2
Example
y2<-sample(0:2,120,replace=TRUE) y2
Output
[1] 2 1 1 1 0 0 1 1 1 0 1 0 0 1 2 2 1 0 2 0 0 1 2 1 0 2 1 0 2 0 2 0 2 2 2 0 1 [38] 2 2 0 0 0 1 2 2 1 2 1 2 2 1 0 1 0 0 1 2 1 0 2 2 2 0 0 0 2 0 0 1 2 0 2 1 2 [75] 1 2 1 2 1 2 1 2 2 0 1 2 1 2 0 0 0 0 0 0 1 1 2 1 1 0 0 2 2 1 2 0 1 0 2 0 0 [112] 2 0 2 0 0 2 0 1 1
Example
x2[-match(y2,x2)]
Output
[1] 3 3 4 3 0 4 3 0 2 0 1 3 1 5 2 3 1 1 2 5 2 0 0 3 1 3 0 3 0 0 0 3 2 4 0 0 2 [38] 5 5 0 1 3 1 2 5 5 2 2 4 1 5 3 3 3 4 1 4 0 1 4 1 3 3 4 0 1 1 0 3 1 4 5 2 5 [75] 1 3 4 3 3 1 3 3 2 1 2 3 4 0 1 1 5 4 0 4 1 3 0 0 0 2 1 3 2 4 5 2 5 3 5 0 4 [112] 5 4 1 0 0 2
Example
x3<-sample(1:10,120,replace=TRUE) x3
Output
[1] 5 4 5 1 5 1 8 10 7 3 6 4 2 5 7 9 8 8 7 8 1 6 10 1 9 [26] 4 1 3 4 1 5 1 3 4 6 3 1 3 6 8 3 9 10 7 5 7 5 4 8 9 [51] 8 6 10 8 1 9 5 9 2 3 4 9 5 7 3 10 5 4 8 8 6 10 3 1 8 [76] 2 1 9 10 9 10 10 9 8 5 7 9 5 4 10 10 10 5 6 10 7 10 5 9 7 [101] 10 1 4 5 2 4 1 3 5 4 5 9 9 5 9 5 5 9 7 4
Example
y3<-sample(1:5,120,replace=TRUE) y3
Output
[1] 3 1 4 4 2 1 5 5 5 1 1 2 5 5 3 1 4 5 4 1 2 1 2 5 5 3 2 4 4 5 1 2 1 2 5 5 3 [38] 1 3 1 1 1 1 3 5 5 5 2 1 4 2 1 5 4 2 4 2 1 3 1 4 2 1 2 4 2 3 3 3 5 3 4 1 2 [75] 3 3 4 4 5 2 2 1 1 4 3 2 3 1 3 1 1 5 2 5 1 1 1 1 5 2 5 5 2 3 4 4 4 4 3 2 3 [112] 4 5 3 2 2 4 4 2 4
Example
x3[-match(y3,x3)]
Output
[1] 5 5 1 8 10 7 6 4 5 7 9 8 8 7 8 1 6 10 1 9 4 1 3 4 1 [26] 5 1 3 4 6 3 1 3 6 8 3 9 10 7 5 7 5 4 8 9 8 6 10 8 1 [51] 9 5 9 2 3 4 9 5 7 3 10 5 4 8 8 6 10 3 1 8 2 1 9 10 9 [76] 10 10 9 8 5 7 9 5 4 10 10 10 5 6 10 7 10 5 9 7 10 1 4 5 2 [101] 4 1 3 5 4 5 9 9 5 9 5 5 9 7 4
Example
x4<-sample(1:20,120,replace=TRUE) x4
Output
[1] 10 9 3 15 2 7 5 2 14 3 6 7 11 14 20 20 13 20 13 20 6 4 11 5 4 [26] 10 19 8 5 3 12 17 10 10 15 8 16 6 18 8 4 16 13 7 2 2 12 18 9 13 [51] 12 12 6 11 10 7 5 12 10 5 8 11 8 7 11 9 18 8 17 20 3 4 8 3 8 [76] 17 5 11 14 10 4 12 5 20 17 18 16 6 4 7 2 4 10 1 5 14 13 6 3 9 [101] 2 15 14 13 16 10 9 20 5 1 10 16 3 11 16 9 20 8 19 9
Example
y4<-sample(1:10,120,replace=TRUE) y4
Output
[1] 4 9 1 10 2 8 4 3 7 7 8 5 10 2 10 5 9 4 5 8 3 4 7 3 5 [26] 7 2 8 5 4 3 5 8 5 1 10 4 3 7 6 4 10 2 1 9 7 9 8 10 5 [51] 6 1 8 5 5 6 3 7 6 8 5 3 8 7 6 5 6 5 10 7 10 1 5 3 2 [76] 2 5 9 9 7 5 2 9 4 10 7 5 6 8 7 4 10 8 7 6 7 9 5 10 8 [101] 9 1 1 8 5 2 1 4 4 8 7 8 1 1 2 7 8 1 5 7
Example
x4[-match(y4,x4)]
Output
[1] 15 2 14 3 7 11 14 20 20 13 20 13 20 6 11 5 4 10 19 5 3 12 17 10 10 [26] 15 8 16 6 18 8 4 16 13 7 2 2 12 18 9 13 12 12 6 11 10 7 5 12 10 [51] 5 8 11 8 7 11 9 18 8 17 20 3 4 8 3 8 17 5 11 14 10 4 12 5 20 [76] 17 18 16 6 4 7 2 4 10 5 14 13 6 3 9 2 15 14 13 16 10 9 20 5 1 [101] 10 16 3 11 16 9 20 8 19 9
Example
x5<-rpois(120,5) x5
Output
[1] 5 6 7 3 6 9 3 5 3 7 2 5 5 4 7 7 6 2 5 7 6 7 8 4 6 [26] 1 2 6 3 3 5 6 4 8 3 5 2 9 4 6 4 6 2 3 11 6 6 8 4 5 [51] 11 4 8 5 4 4 5 1 5 5 5 8 3 7 2 8 3 4 3 5 5 5 4 3 4 [76] 5 2 8 7 5 6 4 3 3 3 7 0 7 3 4 3 5 8 7 12 5 8 2 7 3 [101] 4 10 1 4 3 5 1 8 6 6 4 12 5 4 3 3 1 6 11 5
Example
y5<-rpois(120,3) y5
Output
[1] 3 7 2 2 5 1 5 4 1 5 1 5 3 3 6 3 4 2 5 4 2 6 0 4 5 1 4 5 3 3 4 3 4 2 1 7 4 [38] 5 5 2 3 4 8 4 5 3 3 2 0 7 1 0 4 6 3 3 3 4 5 1 1 2 3 4 5 3 2 2 5 3 0 7 2 1 [75] 5 3 2 1 4 3 5 2 0 6 4 4 0 3 0 5 2 3 1 3 2 3 2 4 4 2 4 4 4 2 1 2 4 2 4 3 2 [112] 4 5 6 3 1 4 2 2 4
Example
x5[-match(y5,x5)]
Output
[1] 6 9 3 5 3 7 5 5 7 7 6 2 5 7 6 7 4 6 2 6 3 3 5 6 4 [26] 8 3 5 2 9 4 6 4 6 2 3 11 6 6 8 4 5 11 4 8 5 4 4 5 1 [51] 5 5 5 8 3 7 2 8 3 4 3 5 5 5 4 3 4 5 2 8 7 5 6 4 3 [76] 3 3 7 7 3 4 3 5 8 7 12 5 8 2 7 3 4 10 1 4 3 5 1 8 6 [101] 6 4 12 5 4 3 3 1 6 11 5
- Related Articles
- How to find the frequency vector elements that exists in another vector in R?
- How to replace one vector elements with another vector elements in R?
- How to replicate a vector to create matrix in R?
- How to find the position of one or more values in a vector into another vector that contains same values in R?
- How to remove some last elements of a vector in R?
- How to find unique permutations if a vector contains repeated elements in R?
- How to extract strings that contains a particular substring in an R vector?
- How to match the names of a vector in sequence with string vector values in another vector having same values in R?
- How to sort a vector in increasing order that contains numbers and characters in R?
- How to remove names from a named vector in R?
- How to print the vector elements vertically in R?
- How to find similar words in vector of strings in R?
- How to create a frequency table of a vector that contains repeated values in R?
- How to remove double inverted commas in R vector?
- How to generate a normal random vector using the mean of a vector in R?

Advertisements