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