R Basic

is.element() Function: Check Presence of Elements in R

Whether you want to do membership testing, filter data, identify missing values, check for duplicates, or conditional operations, you have to check if an element or object is present within the other element or object. At this juncture, is.element() function asserts its relevance.

is.element()

The is.element() is a built-in R function that checks the presence of an element or object within another object. It returns TRUE for the element that has found a match and FALSE otherwise. It returns a logical vector of the same length as the first argument.

For example, if your input vector has length 4 then the output of is.element() function has 4 lengths, and its elements are either TRUE or FALSE based on the matches.

Syntax

is.element(input_obj, data_structure)

Parameters

Name Value
input_obj It is an input element that we want to search for in “data_structure”.
data_structure It is an R object that will act as a data structure in which we search for an input_obj.

Visual representation

Numeric comparisons

If you are comparing numeric vectors, keep in mind that the is.element() function performs precise comparisons. Even a slight difference in decimal places can lead to a FALSE result.

num_vec_one <- c(11, 19, 21)
r_obj <- c(11, 19, 21, 18, 6)

is.element(num_vec_one, r_obj) # TRUE  TRUE  TRUE

Here, you can see that r_obj contains all the elements of the num_vec_one numeric vector.

If you check out the length of the output, then it is three. Why? because the input vector num_vec_one has three elements.

Let’s check out another scenario using a vector filled with decimal values.

num_decimal <- c(11.1, 19.21, 3.1419)
r_obj <- c(11.1, 19.21, 3.14195)

is.element(num_decimal, r_obj) # TRUE TRUE FALSE

As you can see from the output, the third element in num_decimal is not precisely equal to the third element in r_obj, so the result is FALSE. 

In num_decimal, after a decimal point, the length of a number is 4. However, in r_obj, after the decimal point, the length of the number is 5So, we get the FALSE as a result of the third element.

Character comparisons

The is.element() function compares characters by characters of input strings or character vectors in the other R object. If each character is matched, it returns TRUE; otherwise, it returns FALSE. Even if the character’s case does not match, it returns FALSE. So, it strictly checks the element in the object.

string_val <- "Krunal"
r_obj <- c("Krunal", "Ankit")

is.element(string_val, r_obj) # TRUE

You can see that the “Krunal” string appears in r_obj as it is.

Let’s change the case of input string_val and see the output.

string_val <- "KRUNAL"
r_obj <- c("Krunal", "Ankit")

is.element(string_val, r_obj) # FALSE

Once I changed the case of the input value to uppercase, it did not match in the r_obj, and hence, it returned FALSE.

List

You can compare two nested lists of each element with each other and find out which elements are the same.

shares_list_one <- list(c("Delton", "VishalMegaMart"), c("DixonTech"))
shares_list_two <- list(c("Polycab", "VishalMegaMart"), c("KaynesTech"))

result <- sapply(seq_along(shares_list_one), function(i) {
   is.element(shares_list_one[[i]], shares_list_two[[i]])
})

print(result)

# [[1]]
# [1] FALSE TRUE

# [[2]]
# [1] FALSE 

You can see that the shares_list_one list has a “VishalMegaMart” element, and the shares_list_two also has a “VishalMegaMart” element. That means is.element() function returns TRUE for only that element because shares_list_two has that element, and the rest element returns FALSE.

Factor

In Factor type, is.element() function checks the levels of the first object into the second object. Only levels will be checked.

fact_one <- factor(c("a", "b", "c"))
fact_two <- factor(c("a", "b", "d"))

is_in_fact_two <- is.element(levels(fact_one), levels(fact_two))

print(is_in_fact_two)

# TRUE  TRUE  FALSE

You can see that not all the elements of fact_one are included in fact_two, and hence, the third level of fact_one’s “c” is not in fact_two, and it returns FALSE.

Recent Posts

R length(): Vector, List, Matrix, Array, Data Frame, String

Before executing an operation on an object, it is advisable to check its length, as…

1 day ago

How to Round Numbers in R

Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…

2 days ago

Adding Single or Multiple Columns to Data Frame in R

Whether you want to add new data to your existing datasets or create new variables…

4 days ago

sqrt() Function: Calculate Square Root in R

The square root of a number is a value that is multiplied by itself, giving…

5 days ago

How to Remove Duplicate Rows from DataFrame in R

Duplicate rows refer to all the values across all columns that are the same in…

7 days ago

How to Remove NA From Vector in R

A vector is a data structure that holds the same type of data. When working…

1 week ago