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 5. So, 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.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.