contains in r [how to test if a vector contains a specific element]

When programming in R, sometimes it is necessary to check to see if a vector contains a particular value. Not only can you check to see if a vector contains that value, but you can also find out where it is. Finding both answers requires using two simple functions that are easy to use.

Description (Contains in R)

Checking what a vector contains in r is easy, but there are two ways of looking for a specific value. The first is the search %in% vector operation which checks to see if the vector contains the search. There is also the match() function which has the form of match(search, vector) and it returns the position of the search in the vector. The search argument can be a character string, numeric value, or any other data type. It even works with missing values in the form of NA. The %in% operation returns a Boolean which is true if the argument being searched for is present. The match() function simply returns the numeric position of the argument being searched for, and it returns an NA if it is not present.

Explanation – Why We Use These Operators / Functions

When evaluating the contents of data structures, it is important to remember the methods we are discussing only work on vectors. This means that they will only work on the columns of a data frame, but you can use it to find a row number by searching for a value in one of the columns. The search parameter can be a specific text, number, or other data type. These methods can search for a string just as easily as a single character. Once you find the contents that you are looking for you can store this information in a variable to use as necessary in your program.

Examples

Here are three examples of these two operations in action. The first two contain both successful and unsuccessful searches.

> a = c(1:5)
> a
[1] 1 2 3 4 5
> 3 %in% a
[1] TRUE
> match(3,a)
[1] 3
>
> 7 %in% a
[1] FALSE
> match(7,a)
[1] NA

This example uses both methods for looking up a numeric value. In the first case, it is present, but in the second case, it is not.

> b = c(“a”,”b”,”c”,”d”,”e”)
> “d” %in% b
[1] TRUE
> match(“d”,b)
[1] 4
>
> “z” %in% b
[1] FALSE
> match(“z”,b)
[1] NA

This example uses both methods for looking up a character value. In the first case, it is present, but in the second case, it is not.

> b = c(“a”,”b”,NA,”d”,”e”)
> NA %in% b
[1] TRUE
> match(NA,b)
[1] 3

This final example uses both methods for looking up an NA value and finds it.
<h2>Application</h2>The main application of these methods is determining whether the vectors contain specific values and where those values are located. This is useful in finding the pattern of data within a data structure you are looking at. The regular expression of these methods would include a variable that would store the results, but you can send the results directly to the console if you need to see them.

If you need to find specific values within a data structure and it contains vectors these two methods will work. Furthermore, they can help you find other data that is related to the parameter that you are looking up. If your program needs to find a specific value these methods will do the job.

Scroll to top