检查 R 编程中对象之间是否存在公共元素 – is.element()函数
R 语言中的is.element()
函数用于检查第一个对象的元素是否存在于第二个对象中。它为每个相等的值返回 TRUE。
Syntax: is.element(x, y)
Parameters:
x and y: Objects with sequence of items
示例 1:
# R program to illustrate
# the use of is.element() function
# Vector 1
x1 <- c(1, 2, 3)
# Vector 2
x2 <- c(1:6)
# Calling is.element() Function
is.element(x1, x2)
is.element(x2, x1)
输出:
[1] TRUE TRUE TRUE
[1] TRUE TRUE TRUE FALSE FALSE FALSE
示例 2:
# R program to illustrate
# the use of is.element() function
# Data frame 1
data_x <- data.frame(x1 = c(5, 3, 7),
x2 = c(1, 4, 2))
# Data frame 2
data_y <- data.frame(y1 = c(2, 3, 4),
y2 = c(1, 4, 2),
y3 = c(3, 4, 5))
# Calling is.element() Function
is.element(data_x, data_y)
is.element(data_y, data_x)
输出:
[1] FALSE TRUE
[1] FALSE TRUE FALSE