Comparing Elements of Two Arrays in R
Overview
R is frequently used in fields more interested in the content of data rather than its form or structure, thus comparing this aspect is also useful.
Inclusion
(It’s not important at all, but in the examples, A represents triangular numbers $\displaystyle {{n(n+1)} \over {2}}$ and B represents square numbers $m^2$.)
Using the binary operator %in%
to compare two arrays returns true for elements of A that are also in B, and false otherwise. This can also be used to search for letters or words by splitting a string with the strsplit() function and its applications.
Intersection and Symmetric Difference
The intersect()
function and setdiff()
function calculate the $A \cap B$ and $A \triangle B = ( A \cup B ) \setminus ( A \cap B )$, respectively. (Again, it’s not important at all, but $1$ and $36$ can be recognized as numbers that are both triangular and square.)
Code
A<-((1:14)*(2:15))/2; A
B<-(1:10)^2; B
A %in% B
c("은") %in% strsplit("달변은 은이요 침묵은 금이다","")[[1]]
intersect(A,B)
setdiff(A,B)