I am trying to use dplyr
to do the following:
tapply(iris$Petal.Length, iris$Species, shapiro.test)
I want to split the Petal.Lengths by Speicies, and apply a function, in this case shapiro.test.
I read this SO question and quite a number of other pages. I am sort of able to split the variable into groups, using do
:
iris %>%
group_by(Species) %>%
select(Petal.Length) %>%
do(print(.$Petal.Length))
[1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 1.5 1.6 1.4 1.1 1.2
[16] 1.5 1.3 1.4 1.7 1.5 1.7 1.5 1.0 1.7 1.9 1.6 1.6 1.5 1.4 1.6
[31] 1.6 1.5 1.5 1.4 1.5 1.2 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9
[46] 1.4 1.6 1.4 1.5 1.4
[1] 4.7 4.5 4.9 4.0 4.6 4.5 4.7 3.3 4.6 3.9 3.5 4.2 4.0 4.7 3.6
[16] 4.4 4.5 4.1 4.5 3.9 4.8 4.0 4.9 4.7 4.3 4.4 4.8 5.0 4.5 3.5
[31] 3.8 3.7 3.9 5.1 4.5 4.5 4.7 4.4 4.1 4.0 4.4 4.6 4.0 3.3 4.2
[46] 4.2 4.2 4.3 3.0 4.1
The 'splitting' of the column into groups seems to be working. But the way to pass the pieces to shapiro.test is still eluding me. I see that group_by
is different from split into.
I tried lots of variations, including:
iris %>%
group_by(Species) %>%
select(Petal.Length) %>%
summarise(shapiro.test)
and also
iris %>%
group_by(Species) %>%
select(Petal.Length) %>%
summarise_each(funs(shapiro.test))
# Error: expecting a single value
How can I make dplyr
run shapiro.test()
thrice, once for the Petal.Lengths of each Species?