To get the length of a set in R, you can use the length
function.
In this example,
my_set
using the c
function, which combines multiple elements into a vector.unique
function, which ensures all elements are unique.length
function to get the number of elements in the set.print
function.my_set <- unique(c('apple', 'banana', 'orange'))
# Get the length of the set
length_of_set <- length(my_set)
# Print the length of the set
print(paste('Length of the set:', length_of_set))
Length of the set: 3
In this example,
empty_set
using the c
function with no elements.unique
function to ensure it is treated as a set (though it will still be empty).length
function to get the number of elements in the set.print
function.empty_set <- unique(c())
# Get the length of the empty set
length_of_empty_set <- length(empty_set)
# Print the length of the empty set
print(paste('Length of the empty set:', length_of_empty_set))
Length of the empty set: 0
In this tutorial, we learned How to Get Length of a Set in R language with well detailed examples.