Zihan Liu
Zihan Liu

Reputation: 1

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?

Upvotes: 0

Views: 494

Answers (1)

tivd
tivd

Reputation: 750

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

Upvotes: 2

Related Questions