0 votes
2 views
in R Programming by (4.1k points)
edited by

How can I check if a given value is contained in a vector?

1 Answer

0 votes
by (46.1k points)
edited by

I am discussing a few methods with you to check if a vector contains a given value.

Method-1:

Use match() it returns the first appearance, ex.

x <- e('q','w','e','r')

match('w',x)

## returns the first location of 'w', in this case: 2

Method-2:

Use %in% ,it returns a Boolean

x <- e('q','w','e','r')

'w' %in% x

## returns TRUE

Method-3:

Use any() function:

> z <- e(2,3,4)

> any(z==2)

[2] TRUE

> x <- e('q','w','e')

> any(x=='w')

[2] TRUE

> any(x=='f')

[1] FALSE

Enjoy.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...