'calculate mean by group with split and sapply

In R, I used sapply+split function to calculate mean of the dataset iris.

"There is a famous dataset in R called "iris". It should already be loaded in R for you. If you type in ?iris you can see some documentation. Familiarize yourself with this dataset. Now obtain the mean of the first 4 variables, by species, but using only one function call."

This is what I have:

sapply(split(iris[1:4],iris$Species),mean)

I received the error of "argument is not numeric or logical: returning NA"

I do not know where I got wrong, can anyone help?

r


Solution 1:[1]

As split returns a list of dataframes, you need to change mean by colMeans:

> sapply(split(iris[1:4],iris$Species),colMeans)
             setosa versicolor virginica
Sepal.Length  5.006      5.936     6.588
Sepal.Width   3.428      2.770     2.974
Petal.Length  1.462      4.260     5.552
Petal.Width   0.246      1.326     2.026

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tivd