Your question is really about how to subset a data frame.
Here is one way:
mean(iris$Sepal.Length[iris$Species=="virginica"])
[1] 6.588
You can rewrite this with less duplication by using the function with()
:
mean(with(iris, Sepal.Length[Species=="virginica"]))
[1] 6.588
And another way:
mean(with(iris, iris[Species=="virginica", "Sepal.Length"]))
[1] 6.588