1 Summary


A possible exercise of data manipulation is to filter all the rows where the fourth letter of a name is an o or an s.

In this example we are going to filter the species from the iris dataset.


2 Data import


# We are going to 
data(iris)
summary(iris$Species)
##     setosa versicolor  virginica 
##         50         50         50

3 Solution


# We use the substring function to compare the 4th letter to o and s.
iris_filtered <- iris[substring(iris$Species,4,4) %in% c("o","s"),] 
table(iris_filtered$Species)
## 
##     setosa versicolor  virginica 
##         50         50          0