To create an empty set in R, you can use the set() function without passing any initial elements.
In this example,
set()
function to create an empty set in R without specifying any initial elements.# Create an empty set
empty_set <- set()
# empty_set is now ready to be used as an empty set
# Example usage:
# Add elements to the set
empty_set <- union(empty_set, c(5))
# Remove elements from the set
empty_set <- setdiff(empty_set, c(5))
# Check if an element is present in the set
contains_element <- 5 %in% empty_set
print(paste('Is element 5 present in the set:', contains_element))
[1] "Is element 5 present in the set: FALSE"
In this example,
set()
.set()
function.# Create an empty set using set literal
empty_set_literal <- set()
# empty_set_literal is now ready to be used as an empty set
# Example usage:
# Add elements to the set
empty_set_literal <- union(empty_set_literal, c("apple"))
# Remove elements from the set
empty_set_literal <- setdiff(empty_set_literal, c("apple"))
# Check if an element is present in the set
contains_element <- "apple" %in% empty_set_literal
print(paste('Is element 'apple' present in the set:', contains_element))
[1] "Is element 'apple' present in the set: FALSE"
In this tutorial, we learned How to create an Empty Set in R language with well detailed examples.