To remove items from a set based on a condition in R, you can use the subset function along with a logical condition to filter out the items.
In this example,
number_set
containing integer values 1
, 2
, 3
, 4
, and 5
.subset
function with a logical condition that checks for odd numbers to keep them in the set.number_set
to see the removed items.number_set <- c(1, 2, 3, 4, 5)
number_set <- subset(number_set, number_set %% 2 != 0)
print(number_set)
[1] 1 3 5
In this example,
string_set
containing string values 'apple'
, 'banana'
, 'cherry'
, 'date'
, and 'grape'
.subset
function with a logical condition that checks for string length greater than or equal to 6 characters to keep them in the set.string_set
to see the removed items.string_set <- c('apple', 'banana', 'cherry', 'date', 'grape')
string_set <- subset(string_set, nchar(string_set) >= 6)
print(string_set)
[1] "banana" "cherry"
In this tutorial, we learned How to Remove Items from Set based on a Condition in R language with well detailed examples.