I'm having some issues using a function that should calculate the weighted arithmetic mean in R.
I am using the following function to calculate the weighted arithmetic means for some scores:
candidateScores = function(weight1, weight2, weight3) {
if(weight1+weight2+weight3 == 1) {
BobScore = 9*weight1 + 6*weight2 + 4*weight3
SueScore = 7*weight1 + 7*weight2 + 6*weight3
HyolynScore = 4*weight1 + 8*weight2 + 8*weight3
print(paste("Bob's Score: ", BobScore))
print(paste("Sue's Score: ", SueScore))
print(paste("Hyolyn's Score: ", HyolynScore))
} else {
print("Your weights do not add up to 1.")
}
}
It is for use with aggregate functions so the weights must total 1, hence the "else" condition. For most values the function works correctly, however for a few value combinations I get the "else" output, despite the weights totally 1.
Some combinations that cause this:
-> candidateScores(weight1 = 0.6, weight2 = 0.3, weight3 = 0.1)
-> candidateScores(weight1 = 0.31, weight2 = 0.59, weight3 = 0.1)
If I change the function to <=1 rather than ==1, it seems to work with these values.
Does anyone know why this might be the case?
Thanks! And I'm brand new to R so apologies if there are mistakes etc!