Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell what is in one vector and not another?

Tags:

r

vector

In matlab there is a way to find the values in one vector but not in the other.

for example:

x <- c(1,2,3,4) y <- c(2,3,4) 

is there any function that would tell me that the value in x that's not in y is 1?

like image 936
Tony Stark Avatar asked Dec 03 '09 06:12

Tony Stark


People also ask

How to find values not in a vector R?

If we want all the elements of a vector that are not in another vector then we can use setdiff() method in R. It takes two vectors and returns a new vector with the elements of the first vector that are not present in the second vector. Example 1: R.

How do you find the common element in two vectors in R?

First of all, create a number of vectors. Use intersect function to find the common elements in all the vectors.

How do you know if its a vector?

TF = isvector( A ) returns logical 1 ( true ) if A is a vector. Otherwise, it returns logical 0 ( false ).

What is the IN operator in R?

The %in% operator in R can be used to identify if an element (e.g., a number) belongs to a vector or dataframe. For example, it can be used the see if the number 1 is in the sequence of numbers 1 to 10.


1 Answers

you can use the setdiff() (set difference) function:

> setdiff(x, y) [1] 1 
like image 133
Xela Avatar answered Sep 25 '22 10:09

Xela