How to find the frequency vector elements that exists in another vector in R?


If a vector value exists in another vector then we might want to find the frequency/count for such values in the other vector. For example, if we have two vectors say x and y, and some of the values in y exists in x as well. Therefore, we can find the frequency of values in x for y values can be found by using the command colSums(outer(x,y,"==")).

Example

 Live Demo

x1<-LETTERS[1:10]
x1

Output

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"

Example

 Live Demo

y1<-LETTERS[5:15]
y1

Output

[1] "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O"

Example

colSums(outer(x1,y1,"=="))

Output

[1] 1 1 1 1 1 1 0 0 0 0 0

Example

 Live Demo

x2<-sample(LETTERS[1:10],100,replace=TRUE)
x2

Output

 [1] "D" "D" "E" "J" "H" "J" "E" "G" "B" "B" "G" "D" "E" "D" "F" "G" "B" "H"
[19] "I" "E" "G" "H" "I" "I" "B" "B" "I" "C" "H" "F" "D" "D" "B" "B" "B" "J"
[37] "C" "A" "F" "H" "A" "A" "G" "I" "F" "J" "D" "H" "H" "A" "I" "A" "G" "D"
[55] "C" "H" "E" "A" "J" "D" "H" "A" "A" "F" "E" "A" "G" "G" "I" "C" "A" "D"
[73] "E" "F" "J" "A" "B" "A" "J" "A" "A" "A" "F" "E" "C" "H" "I" "G" "A" "E"
[91] "E" "D" "J" "D" "I" "H" "B" "H" "C" "E"

Example

 Live Demo

y2<-sample(LETTERS[6:12],100,replace=TRUE)
y2

Output

 [1] "K" "I" "L" "L" "K" "F" "G" "F" "K" "L" "G" "G" "H" "I" "I" "K" "I" "F"
[19] "G" "L" "K" "J" "L" "I" "I" "I" "F" "K" "H" "J" "I" "L" "F" "L" "L" "H"
[37] "K" "H" "H" "F" "H" "J" "F" "L" "I" "I" "L" "F" "L" "F" "K" "K" "F" "F"
[55] "J" "H" "I" "F" "K" "J" "F" "J" "I" "I" "F" "I" "G" "F" "I" "G" "H" "K"
[73] "G" "I" "K" "H" "F" "L" "F" "H" "L" "I" "H" "G" "F" "G" "I" "G" "L" "F"
[91] "I" "F" "H" "J" "J" "F" "K" "J" "G" "I"

Example

colSums(outer(x2,y2,"=="))

Output

 [1] 0 9 0 0 0 7 9 7 0 0 9 9 12 9 9 0 9 7 9 0 0 8 0 9 9
[26] 9 7 0 12 8 9 0 7 0 0 12 0 12 12 7 12 8 7 0 9 9 0 7 0 7
[51] 0 0 7 7 8 12 9 7 0 8 7 8 9 9 7 9 9 7 9 9 12 0 9 9 0
[76] 12 7 0 7 12 0 9 12 9 7 9 9 9 0 7 9 7 12 8 8 7 0 8 9 9

Example

 Live Demo

x3<-sample(rpois(5,2),200,replace=TRUE)
x3

Output

 [1] 3 0 2 2 3 0 1 3 0 1 2 3 3 3 1 3 0 3 1 2 0 2 1 0 0 0 1 3 0 3 3 2 1 2 2 1 3
[38] 3 1 0 2 1 0 3 0 0 2 1 3 3 1 2 0 1 0 3 2 3 0 0 0 2 3 2 0 2 1 3 3 2 1 2 3 3
[75] 1 3 2 3 3 3 2 3 3 2 1 3 3 0 3 0 3 0 0 1 1 3 3 2 3 2 0 2 0 2 0 0 3 2 3 2 0
[112] 3 0 3 2 0 0 2 3 3 3 1 3 3 1 3 0 0 1 0 3 3 2 0 2 3 2 3 2 0 3 2 3 0 3 2 1 0
[149] 0 1 3 2 3 0 3 3 2 3 3 2 3 2 3 0 3 0 1 1 3 0 2 1 0 3 0 2 3 1 2 2 3 3 1 3 1
[186] 0 3 3 2 0 2 3 3 1 1 2 3 2 2 0

Example

 Live Demo

y3<-sample(rpois(3,2),200,replace=TRUE)
y3

Output

 [1] 0 0 0 2 2 0 2 2 0 2 0 2 0 2 2 0 2 0 2 0 0 2 2 2 0 0 2 0 0 0 2 0 0 0 0 2 0
[38] 2 0 2 0 0 0 2 2 0 0 2 2 2 0 2 0 0 0 0 0 0 0 0 0 0 0 2 0 2 2 2 0 0 0 0 0 2
[75] 2 0 2 0 0 2 0 0 2 0 0 2 2 0 2 2 0 0 2 0 0 2 0 0 0 2 2 0 0 2 2 2 0 0 0 2 0
[112] 2 0 2 0 0 0 0 0 2 2 2 2 0 0 0 0 0 0 0 0 2 0 0 0 0 2 2 2 0 2 2 2 0 2 0 0 0
[149] 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 0 0 0 2 0 0 2 0 0 0 0 0 2 0 0 2
[186] 0 2 0 2 2 2 2 0 2 2 2 2 0 2 0

Example

colSums(outer(x3,y3,"=="))

Output

  [1] 48 48 48 47 47 48 47 47 48 47 48 47 48 47 47 48 47 48 47 48 48 47 47 47 48
 [26] 48 47 48 48 48 47 48 48 48 48 47 48 47 48 47 48 48 48 47 47 48 48 47 47 47
 [51] 48 47 48 48 48 48 48 48 48 48 48 48 48 47 48 47 47 47 48 48 48 48 48 47 47
 [76] 48 47 48 48 47 48 48 47 48 48 47 47 48 47 47 48 48 47 48 48 47 48 48 48 47
[101] 47 48 48 47 47 47 48 48 48 47 48 47 48 47 48 48 48 48 48 47 47 47 47 48 48
[126] 48 48 48 48 48 48 47 48 48 48 48 47 47 47 48 47 47 47 48 47 48 48 48 48 48
[151] 48 48 47 48 48 48 48 48 48 48 48 48 48 48 48 48 47 48 47 48 48 48 47 48 48
[176] 47 48 48 48 48 48 47 48 48 47 48 47 48 47 47 47 47 48 47 47 47 47 48 47 48

Example

 Live Demo

x4<-sample(rnorm(5),100,replace=TRUE)
x4

Output

 [1] -0.2818766  0.7100732  0.1288669  1.4954190  0.7100732  1.4954190
[7]   1.4954190  1.1059633 -0.2818766  0.7100732 -0.2818766  1.1059633
[13] -0.2818766  0.1288669  0.1288669  0.7100732  0.7100732  0.1288669
[19]  0.1288669  0.1288669  1.4954190  0.7100732 -0.2818766  1.4954190
[25]  1.1059633  0.7100732  1.1059633  1.1059633  1.1059633 -0.2818766
[31]  1.1059633 -0.2818766  1.4954190  1.1059633  1.1059633  1.4954190
[37]  1.1059633  0.1288669  1.4954190  1.1059633  0.7100732  0.7100732
[43]  1.1059633  1.1059633  1.4954190  1.4954190  0.7100732  0.1288669
[49]  1.4954190  1.1059633  1.4954190  1.4954190  0.1288669  1.4954190
[55] -0.2818766  0.7100732 -0.2818766  1.1059633  0.7100732  1.1059633
[61] -0.2818766  0.1288669  0.7100732 -0.2818766 -0.2818766 -0.2818766
[67]  0.1288669  1.4954190  0.1288669  1.1059633  0.7100732  0.7100732
[73]  1.4954190  0.1288669 -0.2818766 -0.2818766 -0.2818766  0.7100732
[79] -0.2818766  1.1059633 -0.2818766 -0.2818766  1.4954190  1.4954190
[85]  0.1288669  0.7100732 -0.2818766  0.7100732  1.4954190  0.7100732
[91] -0.2818766 -0.2818766  0.7100732  0.7100732  0.7100732  0.7100732
[97]  1.4954190 -0.2818766  0.7100732 -0.2818766

Example

 Live Demo

y4<-sample(rnorm(2),100,replace=TRUE)
y4

Output

 [1] -0.4483814 -0.4483814 -0.4483814 -1.0352421 -1.0352421 -1.0352421
 [7] -0.4483814 -1.0352421 -1.0352421 -1.0352421 -0.4483814 -0.4483814
[13] -1.0352421 -0.4483814 -1.0352421 -0.4483814 -1.0352421 -1.0352421
[19] -1.0352421 -1.0352421 -1.0352421 -0.4483814 -1.0352421 -1.0352421
[25] -0.4483814 -1.0352421 -1.0352421 -0.4483814 -0.4483814 -0.4483814
[31] -0.4483814 -0.4483814 -1.0352421 -1.0352421 -1.0352421 -0.4483814
[37] -0.4483814 -1.0352421 -1.0352421 -1.0352421 -0.4483814 -0.4483814
[43] -0.4483814 -0.4483814 -0.4483814 -0.4483814 -1.0352421 -0.4483814
[49] -0.4483814 -0.4483814 -0.4483814 -1.0352421 -1.0352421 -0.4483814
[55] -1.0352421 -1.0352421 -1.0352421 -1.0352421 -1.0352421 -1.0352421
[61] -1.0352421 -0.4483814 -1.0352421 -0.4483814 -1.0352421 -0.4483814
[67] -0.4483814 -1.0352421 -0.4483814 -0.4483814 -0.4483814 -0.4483814
[73] -0.4483814 -1.0352421 -0.4483814 -0.4483814 -1.0352421 -0.4483814
[79] -0.4483814 -0.4483814 -1.0352421 -1.0352421 -0.4483814 -0.4483814
[85] -0.4483814 -0.4483814 -1.0352421 -0.4483814 -0.4483814 -1.0352421
[91] -0.4483814 -0.4483814 -0.4483814 -1.0352421 -0.4483814 -0.4483814
[97] -0.4483814 -1.0352421 -0.4483814 -0.4483814

Example

colSums(outer(x4,y4,"=="))

Output

 [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Example

 Live Demo

x5<-sample(round(rnorm(5),0),100,replace=TRUE)
x5

Output

 [1] 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 1 0
[38] 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1 0 1 1 1 0 0 1 0 1 1 0 1 1 0 0
[75] 0 0 1 0 1 0 1 0 0 1 0 0 1 0 1 1 1 1 1 1 0 1 1 1 1 1

Example

 Live Demo

y5<-sample(round(rnorm(5,2,1),0),100,replace=TRUE)
y5

Output

 [1] 0 2 1 3 2 3 2 0 3 3 3 2 1 2 0 0 0 2 2 2 2 3 0 2 0 2 3 1 2 2 2 0 2 2 2 0 3
[38] 1 2 0 2 1 2 2 3 2 1 1 1 2 3 2 2 3 0 2 0 2 3 2 3 0 0 2 0 0 0 0 0 3 2 2 2 3
[75] 2 0 1 0 2 1 3 1 2 0 0 1 2 0 2 2 0 1 0 2 1 2 2 2 0 2

Example

colSums(outer(x5,y5,"=="))

Output

 [1] 50 0 50 0 0 0 0 50 0 0 0 0 50 0 50 50 50 0 0 0 0 0 50 0 50
[26] 0 0 50 0 0 0 50 0 0 0 50 0 50 0 50 0 50 0 0 0 0 50 50 50 0
[51] 0 0 0 0 50 0 50 0 0 0 0 50 50 0 50 50 50 50 50 0 0 0 0 0 0
[76] 50 50 50 0 50 0 50 0 50 50 50 0 50 0 0 50 50 50 0 50 0 0 0 50 0

Updated on: 11-Feb-2021

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements