-3

I want to create some mean of sepal.length, but divided by species. Using mosaic package

mean(Sepal.Length~Species)

It didn't work

zx8754
  • 52,746
  • 12
  • 114
  • 209
itsmecevi
  • 51
  • 9
  • 4
    Unless `Sepal.Length` and `Species` are objects saved in your environment, you haven't done anything to tell `mean` where that data should be coming from. This is basically a typo – camille Nov 25 '19 at 00:09
  • mosaic:::mean(Sepal.Length ~ Species,data=iris), you still need to provide a dataframe – StupidWolf Nov 26 '19 at 00:24

3 Answers3

2

We need to specify the data

mosaic::mean(Sepal.Length ~ Species, data = iris)
#    setosa versicolor  virginica 
#     5.006      5.936      6.588 
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Using dplyr, you can do:

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
dc37
  • 15,840
  • 4
  • 15
  • 32
1

A base R solution

aggregate(Sepal.Length ~ Species, data=iris, mean)
     Species Sepal.Length
1     setosa        5.006
2 versicolor        5.936
3  virginica        6.588
G5W
  • 36,531
  • 10
  • 47
  • 80