How to compare the values of two vectors in R?

How to compare the values of two vectors in R?, Using match() and %in% we can compare vectors

Today, we’ll talk about comparing two R vectors to see what components (values).

Here, we have two choices:

The indexes of common elements are returned by the match () function in R.

  1. Indicating if a value from the first vector was present in the second.
  2. The percent in percent operation produces a vector of True or False answers.

Finding Values in Vectors using R Match

Starting with the R match() function, let’s go on.

Value<-c(15,13,12,14,12,15,30)

The first position of the matching value is returned by the r match example.

match(12, Value)
[1] 3

Example of an r match with multiple values

match (c(13,12), Value)
[1] 2 3

The initial position of each of the two values is returned by the R match function when we pass it a vector of multiple values.

Operator – Boolean Equivalent for percent in percent

Do you only require a True/False response?

Use the percent in percent operator, if possible. Similar operations are carried out, and the result is returned as a Boolean indicator indicating if the value is present.

For an example of the R percent in percent operator

by utilizing the percent in percent operator, check for a single value.

14 %in% Value
[1] TRUE

Utilize the percent in percent operator to verify a vector of multiple values.

c(10, 12) %in% Value
[1] FALSE  TRUE

what values the percent in percent operator treats when it can’t

c(10, 12, 14) %in% Value
[1] FALSE  TRUE  TRUE

Summary

The percent in percent operator and R Match; We can compare the values of two vectors using these two tools.

You may also like...

2 Responses

  1. Don MacQueen says:

    Interesting!

    “compare” is a very broad concept. Here are some more ways to use basic R functions to compare vectors — first for character vectors, then for numeric.

    ## create two character vectors
    > L1 L1
    [1] “g” “z” “n” “g” “g”
    >
    > L2

    ## what values do they have in common?
    > intersect(L1,L2)
    [1] “g” “z”

    ## what values are in L1 but not in L2
    > setdiff(L1, L2)
    [1] “n”

    ## in L2 but not in L1
    > setdiff(L2, L1)
    [1] “u” “p” “x” “l” “f” “r”
    >

    ## numeric
    > x y
    > cbind(x=range(x), y=range(y))
    x y
    [1,] 0.7332656 6.40427
    [2,] 22.7891377 14.78329

    ## if I want to put this in a report, row names will be useful
    > tmp rownames(tmp) tmp
    x y
    min 0.7332656 6.40427
    max 22.7891377 14.78329

    ## R defaulted to too many decimal places. Show fewer:
    > signif(tmp,3)
    x y
    min 0.733 6.4
    max 22.800 14.8

    ## which inspires:
    > round( cbind(quantile(x), quantile(y)) ,2)
    [,1] [,2]
    0% 0.73 6.40
    25% 6.88 9.11
    50% 8.78 10.58
    75% 12.46 12.27
    100% 22.79 14.78

    ## and R has all the tools needed to build a simple graphical comparison
    > d1 d2 xrng yrng plot(d1, xlim=xrng, ylim=yrng)
    > lines(d2, col=’red’)
    ## still needs a legend, and better title, axis labels, and such
    ## but this is enough to get started

Leave a Reply

Your email address will not be published. Required fields are marked *

13 + 17 =