To get a subset from a Set in R, you can use the subset
function or the square bracket operator []
to filter elements based on a condition.
In this example,
numbers
and initialize it with some integer elements using the c
function.%%
operator to find even numbers and the subset
function to filter these elements.subset
function returns a new vector containing only the elements that satisfy the condition.# Initialize the Set
numbers <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# Get subset of even numbers
even_numbers <- subset(numbers, numbers %% 2 == 0)
# Print the subset
cat('Subset of even numbers:\n')
print(even_numbers)
Subset of even numbers: [1] 2 4 6 8 10
In this example,
numbers
and initialize it with some integer elements using the c
function.[]
to filter elements that are greater than 5.# Initialize the Set
numbers <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# Get subset of numbers greater than 5
numbers_greater_than_five <- numbers[numbers > 5]
# Print the subset
cat('Subset of numbers greater than 5:\n')
print(numbers_greater_than_five)
Subset of numbers greater than 5: [1] 6 7 8 9 10
In this tutorial, we learned How to get Subset from a Set in R language with well detailed examples.