Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect that a vector is subset of specific vector?

Tags:

r

set

vector

subset

I have two vectors (sets) like this:

first<-c(1,2,3,4,5)
second<-c(2,4,5)

how can I detect that whether second is subset of first or not? is there any function for this?

like image 716
Majid Avatar asked Nov 09 '14 17:11

Majid


People also ask

How do you check if a vector is a subset of another vector?

So if you have two instances of the number 99 in vector a, then as long as vector b has at least one instance of the number 99, then it will be declared as a subset.

How do you find the subset of a vector?

The way you tell R that you want to select some particular elements (i.e., a 'subset') from a vector is by placing an 'index vector' in square brackets immediately following the name of the vector. For a simple example, try x[1:10] to view the first ten elements of x.

How do you find the subset of a vector in C++?

Getting a subvector from a vector in C++ Begin Declare s as vector s(vector const &v, int m, int n) to initialize start and end position of vector to constructor. auto first = v. begin() + m. auto last = v.

How do you check if a set is a subset of another set in CPP?

The function std::includes() can be used to determine if one set is a subset of another. The function returns true if and only if each distinct element in the second sorted range has a corresponding distinct element in the first sorted range to which it compares equal.


1 Answers

Here's one way

> all(second %in% first)
[1] TRUE
like image 69
GSee Avatar answered Oct 24 '22 00:10

GSee