Hello! I have two vectors and want to know how many of their elements are equal - what's the best way to do this in R? Best regards, Sven
It depends on what you mean by that. Consider the following two examples: > sum((1:5)==(5:1)) [1] 1 > > sum((2:1) %in%(1:6)) [1] 2 > Does one of these solve your problem? spencer graves Sven C. Koehler wrote:>Hello! > >I have two vectors and want to know how many of their elements are equal - >what's the best way to do this in R? > >Best regards, > >Sven > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
On Sun, 2005-02-27 at 16:07 +0100, Sven C. Koehler wrote:> Hello! > > I have two vectors and want to know how many of their elements are > equal - > what's the best way to do this in R? > > Best regards, > > Sven> a <- 1:10 > b <- 8:20> a[1] 1 2 3 4 5 6 7 8 9 10> b[1] 8 9 10 11 12 13 14 15 16 17 18 19 20 If you just want to know how many elements in 'a' are in 'b':> sum(a %in% b)[1] 3 If you want to know which elements in 'a' are in 'b':> which(a %in% b)[1] 8 9 10 See ?"%in%" and ?which for more information. HTH, Marc Schwartz
After I hit send, I realized an error in:> which(a %in% b)[1] 8 9 10 The above should be:> a[which(a %in% b)][1] 8 9 10 They happen to be the same by coincidence only, given the values that I used. The first syntax returns the _indices_ of the matches, not the actual matched values themselves. Important difference. For example and clarity:> a <- 5:15 > b <- 12:20> a[1] 5 6 7 8 9 10 11 12 13 14 15> b[1] 12 13 14 15 16 17 18 19 20> which(a %in% b)[1] 8 9 10 11> a[which(a %in% b)][1] 12 13 14 15 Sorry for the confusion. Marc
On Sun, 2005-02-27 at 09:50 -0600, Marc Schwartz wrote: [snip]> > a[which(a %in% b)] > [1] 12 13 14 15One more time: The above can be simplified to just:> a[a %in% b][1] 12 13 14 15 Also, if you happen to be comparing floating point numbers in the two vectors, consider that floating point comparisons are inexact due to the way in which floats are represented in binary on computers. For example:> a <- seq(0, 1.5, 0.1) > b <- seq(1.0, 1.5, 0.1)> a[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5> b[1] 1.0 1.1 1.2 1.3 1.4 1.5> a[a %in% b][1] 1.0 1.1 1.3 1.5 Note that in the above, 1.2 and 1.4 are not returned as a result of attempting an exact comparison of these two floating point numbers. Using something like:> a[round(a, 1) %in% round(b, 1)][1] 1.0 1.1 1.2 1.3 1.4 1.5 will work, since we are rounding the floats to one decimal place prior to the comparison. Marc <Going for another cup of coffee...>
On Sun, Feb 27, 2005 at 09:24:54AM -0600, Marc Schwartz wrote:> > which(a %in% b) > [1] 8 9 10 > > See ?"%in%" and ?which for more information.Thanks for all your replies. This was what I actually wanted to use! -S.