0
votes

This question is asking which command to use given the following situation:

Objective: Calculate mean of iris$Sepal.Length.

Constraint: Do not include the iris$Species 'setosa'.

My Work:

data(iris)
levels(iris$Species)

output: setosa, versicolor, and virginica

mean(iris$Sepal.Length, which(iris$Species != 'setosa'))

output: error message 'incompatible dimensions'

---

This demo is a stand-in for my own dataset, where I want to calculate a function for a variable (such as Sepal.Length) that excludes levels from a second variable (such as Species). I believe my personal dataset and this stand-in demo are comparable.

Perhaps which() is not the appropriate command. What is?

1

1 Answers

2
votes

You were close, try this

mean(iris$Sepal.Length[which(iris$Species != 'setosa')])

or

mean(iris$Sepal.Length[iris$Species != 'setosa'])

or

mean(iris[iris$Species!= "setosa", "Sepal.Length"])