I've created this small data frame:
employee <- c('Yossi ','Pitt ','Deepak','Golan')
salary <- c(21000, 23400, 26800,91000)
testd <- data.frame(employee,salary)
When I write:
mean(testd$salary)
I get the right answer : 40550
But when I tried to calculate the mean for salaries that are greater than 25000 I get this outcome.
mean(testd$salary>=25000)
# [1] 0.5
and not 58900 that is made of this calculation: (26800+91000)/2
What have I done wrong?
Try:
mean(testd$salary[testd$salary>=25000])
Actually testd$salary>=25000
is a vector of boolean
values, which are automatically mapped to numeric
type (True = 1
, False = 0
) when you call mean
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments