How to Tell What Is in One Vector and Not Another

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

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

> setdiff(x, y)
[1] 1

Which numbers are present in a vector but not in another one

setdiff(v1, v2)
# [1] 1 3 5 6 8 9 10

How can I determine whether a vector contains another vector respecting order in R?

You can collapse your vector into a regex pattern and use grepl

vec1 <- c("a", "b", "c")
vec2 <- c("a", "b", "c", "d", "e")
grepl(paste(vec1, collapse=".*"), paste(vec2, collapse=""))
# TRUE
vec3 <- c("e", "d", "c", "b", "a")
grepl(paste(vec1, collapse=".*"), paste(vec3, collapse=""))
# FALSE
vec4 <- c("a", "x", "b", "c", "y")
grepl(paste(vec1, collapse=".*"), paste(vec4, collapse=""))
# TRUE

EDIT: Based on G5W's comment, you can add a delimiter in case each element is not a character but might be a short string. The delimiter will break up the entries of your vector

vec5 <- c("a", "b", "c")
vec6 <- c("ab", "c")
vec7 <- c("ab", "e", "c", "d")
grepl(paste(vec5, collapse="-.*"), paste(vec7, collapse="-"))
# FALSE
grepl(paste(vec6, collapse="-.*"), paste(vec7, collapse="-"))
# TRUE

How to check whether a vector is subset of another one in R?

You can test if length is == and if any values of A have a 1 on positions where B has a 1 and combine the conditions with &&.

length(A) == length(B) && any(A[B==1]==1)
#[1] TRUE

Fulfilling the condition in the original question: A and B have the same length and thus have the same number of elements; yet, A is a subset of B, since A has elements 1 in the same place as B.

To fulfill:

  • A and B have the same length and thus have the same number of elements;
  • A should have at least one 1 at places wherever B has 1
  • A can have only 0s at all places where B has 0s.
length(A) == length(B) && any(A[B==1]==1) && all(A[B==0]==0)

To fulfill:

  • A and B have the same length and thus have the same number of elements;
  • A can have only 0s at all places where B has 0s.
length(A) == length(B) && all(A[B==0]==0)

C++ How to check if contents of vector exist in another vector?

Use a loop that iterates over the contents of the first vector. You don't need a loop for the second vector, just use std::find.

for (auto a_elt: a) {
if (std::find(b.begin(), b.end(), a_elt) == b.end()) {
return false;
}
}
return true;

You can also use std::all_of:

return std::all_of(a.begin(), a.end(), [](int a_elt) {
return std::find(b.begin(), b.end(), a_elt) != b.end();
});

How to find indices of element in one vector in other vector R

a <-  c('Q1', 'Q2', 'Q3')
b <- c('Q10', 'Q13', 'Q1', 'Q1', 'Q40', 'Q2', 'Q2', 'Q2')

which(b %in% a)

[1] 3 4 6 7 8

Julia: Check if elements from one vector are within another vector

There are a number of built-ins that do something similar. indexin gives you the indices in b where the elements of a are found (0 if it is not there - this is similar to R's match). setdiff gives you the elements in a that are not in b. It is likely you'll be able to do what you want with these - constructing temporary boolean arrays for filtering is not so ideomatic in julia as in R, as it generally creates an extra, unnecessary allocation.

Return values not in another vector

To get all values that match, use:

asd[asd %in% asd2]

...returns:

[1] "A"

To get values that aren't in asd2, use this:

asd[!asd %in% asd2]

...returns:

[1] "B" "C"

Test if a vector contains a given element

Both the match() (returns the first appearance) and %in% (returns a Boolean) functions are designed for this.

v <- c('a','b','c','e')

'b' %in% v
## returns TRUE

match('b',v)
## returns the first location of 'b', in this case: 2

Use of which and %in% when no items of a vector match

Using logical operations is more reliable:

b[!b %in% c]
# [1] 1 2
a[!a %in% c]
# [1] 1 2 7 8

Note that !a %in% c is the same as !(a %in% c). In this way we ask which of a are in c, get a logical result, and negate it. Using which, on the other hand, works differently: in -which(a %in% c) we also first get a logical vector a %in% c and then which gives the indices of elements of a that belong to c, and get's rid of those elements. In your case we have

which(a %in% c)
# integer(0)

Then you may argue that a[-numeric(0)] should also return

# [1] 1 2 7 8

but that's not how it is in R.



Related Topics



Leave a reply



Submit