YNWA1992
YNWA1992

Reputation: 99

usage of tapply with FUN = function(x){}

In my task I want to find the mean of temperature by the gender. We can do this by tapply command. Also I am not allowed to use mean command:

tapply(df$temperature,df$gender,FUN = function(x){sum(x)/length(df[,1])})

but it gives me wrong result. It just devide the result by 2, instead of finding mean of both gender. How can I fix that? Because with aggregate function everything works correct:

  aggregate(df$temperature,by = list(df$gender),function(x){sum (x)/length(x)})

Upvotes: 1

Views: 5432

Answers (1)

AOGSTA
AOGSTA

Reputation: 708

Confirming that sum(x)/length(x) is the way to go here:

Creating a mock data set:

set.seed(1)
d<-data.frame(temperature=rnorm(1000,500,20),
              gender=rep(c('M','F'),500))

Calculating mean temperature by gender using tapply with a custom mean function

> tapply(d$temperature, d$gender, function(x){ sum(x)/length(x)})
       F        M 
500.0884 499.4457 

Confirming that this matches the base mean() function:

> mean(d$temperature[d$gender=='F'])
[1] 500.0884
> mean(d$temperature[d$gender=='M'])
[1] 499.4457

Upvotes: 2

Related Questions