I want to get the mean()
and sd()
of the different columns in the dataset iris
according to the value in the column Species
:
> head(iris[order(runif(nrow(iris))), ])
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
50 5.0 3.3 1.4 0.2 setosa
111 6.5 3.2 5.1 2.0 virginica
69 6.2 2.2 4.5 1.5 versicolor
150 5.9 3.0 5.1 1.8 virginica
Without distinguishing among the 3 different species, apply
would do the trick:
> stats = apply(iris[ ,1:4], MARGIN = 2, function(x) rbind(mean(x), SD = sd(x))); row.names(stats) = c("mean", "sd"); stats
Sepal.Length Sepal.Width Petal.Length Petal.Width
mean 5.8433333 3.0573333 3.758000 1.1993333
sd 0.8280661 0.4358663 1.765298 0.7622377
But, How can I get a list (?) with these results broken down by species?