Check Which Elements of a Vector Is Between the Elements of Another One in R

Check which elements of a vector is between the elements of another one in R

You want some variant of findInterval

findInterval(A,B)
[1] 1 3 3 3 3 2

Value 1 indicates it is between 1 and 2 (the lowest and next lowest value in B)
Value 2 indicates it is between 2 and 3

So to find which ones are between

which(findInterval(A,B) %in% seq_len(length(unique(B))-1))
# [1] 1 6

and to extract from A

A[findInterval(A,B) %in% seq_len(length(unique(B))-1)]

# [1] 1.1 2.2

You could also use cut, which will create a factor.

In conjunction with split this will give

 split(A, cut(A,B),drop=FALSE)
$`(1,2]`
[1] 1.1

$`(2,3]`
[1] 2.2

Finding which element of a vector is between two values in R

You are looking for &, not &&:

x = c( .2, .4, 2.1, 5.3, 6.7, 10.5)
y = c( 1, 7)
x = x[ x >= y[1] & x <= y[2]]
x
# [1] 2.1 5.3 6.7

Edited to explain. Here's the text from ?'&' .

& and && indicate logical AND and | and || indicate logical OR. 
The shorter form performs elementwise comparisons in much the same way as arithmetic operators.
The longer form evaluates left to right examining only the first element of each vector.
Evaluation proceeds only until the result is determined.

So when you used && , it returned FALSE for the first element of your x and terminated.

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

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

> setdiff(x, y)
[1] 1

Is between two elements in R

The following gives what you want:

larger <- a[1:length(a)] > b[1:(length(b)-1)]
smaller <- a[1:length(a)] < b[2:length(b)]
between <- larger & smaller
between[1] <- FALSE
a[between]

First you check whether or not the elements in a are smaller than the corresponding elements in b. Then you select if they are smaller than the next element in b. Combine both and remoe the unwanted first. Tada.

Find the first n elements of one vector which contain all the elements of another vector

One option could be:

max(match(vecB, vecA))

Results for different situations:

vecB <- 1:3
vecA <- c(1, 2, 2, 1, 3, 2)

[1] 5

vecB <- 1:3
vecA <- c(3, 2, 2, 1)

[1] 4

vecB <- 1:3
vecA <- c(2, 2, 1)

[1] NA

Determine which elements of a vector partially match a second vector, and which elements don't (in R)

We could use lapply or sapply to loop over the patterns and then get a named output

out <- setNames(lapply(A, function(x) grep(x, B, value = TRUE)), A)

THen, it is easier to check the ones returning empty elements

> out[lengths(out) > 0]
$Cortinarius
[1] "fafsdf_Cortinarius_sdfsdf"

$Russula
[1] "sdfsdf_Russula_sdfsdf_fdf"

> out[lengths(out) == 0]
$Laccaria
character(0)

$Inocybe
character(0)

and get the names of that

> names(out[lengths(out) > 0])
[1] "Cortinarius" "Russula"
> names(out[lengths(out) == 0])
[1] "Laccaria" "Inocybe"

Check the element of a vetcor is between the element of second vector in R

Here's one way.

s <- seq_along(a)  
b[s] < a[s] & a[s] < b[s+1]
# [1] TRUE FALSE TRUE

How to check if a vector is a part other vector in R

You can check with:

sum(!x %in% y)==0

EDIT:

Or as suggested, you can achieve something more readable with

all(x %in% y)

How can I quickly see if any elements of more than 2 vectors are equal in R?

If you put your vectors in a list, they'll be substantially easier to work with:

# make sample data
set.seed(47)
x <- replicate(6, rpois(3, 10), simplify = FALSE)

str(x)
# List of 6
# $ : int [1:3] 16 12 10
# $ : int [1:3] 9 10 6
# $ : int [1:3] 10 14 4
# $ : int [1:3] 7 6 4
# $ : int [1:3] 12 8 7
# $ : int [1:3] 7 11 8

Now iterate with lapply:

lapply(x, function(y){sapply(x, function(z){y %in% z})})

## [[1]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] TRUE FALSE FALSE FALSE FALSE FALSE
## [2,] TRUE FALSE FALSE FALSE TRUE FALSE
## [3,] TRUE TRUE TRUE FALSE FALSE FALSE
##
## [[2]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] FALSE TRUE FALSE FALSE FALSE FALSE
## [2,] TRUE TRUE TRUE FALSE FALSE FALSE
## [3,] FALSE TRUE FALSE TRUE FALSE FALSE
## ... ... ... ... ... ... ...

which is a matrix for each vector, where the rows are the elements of that respective vector and the columns are each of the vectors in the list, and the values indicate whether that element is in that vector. Obviously each will match with itself, so the first column of the first element is all TRUE, as is the second column of the second element, etc. Other TRUEs indicate cross-vector matches. If lengths are inconsistent, it will return a nested list of the same information instead of a matrix. If you'd rather have a nested list anyway, change sapply to lapply.

Alternately, if you just want a vector of matches for each vector,

str(lapply(x, function(y){which(sapply(x, function(z){any(y %in% z)}))}))

## List of 6
## $ : int [1:4] 1 2 3 5
## $ : int [1:4] 1 2 3 4
## $ : int [1:4] 1 2 3 4
## $ : int [1:5] 2 3 4 5 6
## $ : int [1:4] 1 4 5 6
## $ : int [1:3] 4 5 6

where each element still contains itself as a match. Take out which for Booleans instead of indices.

Is there a way to make a logical vector to see if each element of a vector exists within all of the elements of another vector?

EDIT: If I understood correctly you want to know for every element if vec1 if it's contained in other?

library(dplyr)
vec1 <- c("a", "b", "c")

other <- c("a", "b")

logTest <- vec1 %in% other

gives output

r$> logTest                                                                     
[1] TRUE TRUE FALSE


Related Topics



Leave a reply



Submit