鸢尾花数据集 mean() 按物种

Iris dataset mean() by species

我想创造一些sepal.length的意思,但要按物种划分。 Using mosaic 套餐

mean(Sepal.Length~Species)

没用

使用dplyr,您可以:

library(dplyr)
iris %>% group_by(Species) %>% summarise(Mean = mean(Sepal.Length))

# A tibble: 3 x 2
  Species     Mean
  <fct>      <dbl>
1 setosa      5.01
2 versicolor  5.94
3 virginica   6.59

我们需要指定数据

mosaic::mean(Sepal.Length ~ Species, data = iris)
#    setosa versicolor  virginica 
#     5.006      5.936      6.588 

基础 R 解决方案

aggregate(Sepal.Length ~ Species, data=iris, mean)
     Species Sepal.Length
1     setosa        5.006
2 versicolor        5.936
3  virginica        6.588