To filter items of a set based on a condition in R, you can use the subset function or logical indexing to create a new set from the filtered items.
In this example,
numberSet
using the c()
function, which contains numeric values 1
, 5
, 10
, 15
, and 20
.subset
function with a logical condition to filter the numbers greater than 5.filteredSet
.filteredSet
to see the filtered items.numberSet <- c(1, 5, 10, 15, 20)
filteredSet <- subset(numberSet, numberSet > 5)
print(filteredSet)
[1] 10 15 20
In this example,
stringSet
using the c()
function, which contains string values 'apple'
, 'banana'
, 'cherry'
, 'date'
, and 'grape'
.grepl
function with a regular expression to filter the strings containing the letter 'a'.filteredStringSet
.filteredStringSet
to see the filtered strings.stringSet <- c('apple', 'banana', 'cherry', 'date', 'grape')
filteredStringSet <- stringSet[grepl('a', stringSet)]
print(filteredStringSet)
[1] 'apple' 'banana' 'grape'
In this tutorial, we learned How to Filter Items of a Set based on a Condition in R language with well detailed examples.