Lama Kaysi
Lama Kaysi

Reputation: 69

R - tapply column mean, returning logical array

I have a data frame.

I am trying to use the tapply function to find the average of one column when the values of a second column are equal to a given value.

I want tapply to return the value of the mean, but it is returning a logical array

(FALSE - the mean when the values of second column do not equal to the given value, and TRUE - the mean when the values of the second column do equal the given value)

This is the function I am applying (I want the mean of column "hp" when column "cyl" is equal to 4)

tapply(mtcars$hp,mtcars$cyl==4,mean)

This is what is being returned:

 FALSE      TRUE 
180.23810  82.63636 

How can I get the output to simply be 82.63636?

Thank you

Upvotes: 3

Views: 892

Answers (1)

SymbolixAU
SymbolixAU

Reputation: 26258

If you have to use tapply, you need to subset the result to only return the value you want

tapply(mtcars$hp,mtcars$cyl==4,mean)[[2]]
82.63636

However, to get the mean of a column, subset by another colum, you can just call mean on the subsetted data

mean(mtcars$hp[mtcars$cyl == 4])
82.63636

## similarly
# mean(mtcars[mtcars$cyl == 4, "hp"])

Upvotes: 2

Related Questions