To check if a vector contains all the elements of another vector in R, you can use the all
function.
In this example,
vector1
and vector2
with different values.all
function to check if all elements of vector2
are present in vector1
.all
function returns TRUE
if all elements of vector2
are present in vector1
; otherwise, it returns FALSE
.vector1 <- c(1, 2, 3, 4, 5)
vector2 <- c(2, 4)
result <- all(vector2 %in% vector1)
print(paste("Vector 1 contains all elements of Vector 2:", result))
Vector 1 contains all elements of Vector 2: TRUE
In this example,
vector1
and vector2
with different values.all
function to check if all elements of vector2
are present in vector1
.all
function returns TRUE
if all elements of vector2
are present in vector1
; otherwise, it returns FALSE
.vector1 <- c(1, 2, 3, 4, 5)
vector2 <- c(2, 4, 6)
result <- all(vector2 %in% vector1)
print(paste("Vector 1 contains all elements of Vector 2:", result))
Vector 1 contains all elements of Vector 2: FALSE
In this tutorial, we learned How to check if a List contains all the elements of Another List in R language with well detailed examples.