I am trying to find a formula that allows me to find the lowest value for each variable (score...) in a group in order to apply this formula:
relative_socore1= [ team score1 - (worst performing team score 1 (overall)] / [ (Max score 1 per group of teams (e.g. just for group "A")) - worst performing team score1 (overall)]
My data frame has the following structure, with multiple scores....1, 2, 3 and also multiple groups.
data <- data.frame(team= c("blue", "green", "red", "pink", "grey", "black", "rose", "darkblue", "golden", "silver") , group = c("A","A", "B", "C", "D", "D", "D", "F", "F", "F" ), score1 = c(18, 22, 21, 22, 45, 18, 22, 21, 22, 45), score2= c(10, 20, 21, 92, 40, 18, 20, 21, 20, 45), score3 = c(10, 20, 30, 40, 50, 60, 70, 80, 95, 95))
I need to apply this formula to many "scores" in my data frame, changing the "groups" too so that is why I am trying to create a formula instead of calculating it individually.
I know how to get the worst-performing team score 1 (overall),worst_two <- function(x) { min(x, na.rm = T) } but I am struggling a lot to get the value of the best-performing to every group. This part of the formula [ (Max score 1 per group of teams (e.g. just for group "A"))
So far I have managed to make this
test <- function(y, score, data) {
max.score <- max(data[data$group == y, ]$score, na.rm=T)
max.score
}
It works when I specify the $score by the specific name (eg. score1), but it doesn't when I replace the value for an "x", "z" or any other value that would help me to create the function.
#not working for "score1"
test <- function(y, z, data) {
max.score <- max(data[data$group == y, ]$z, na.rm=T)
max.score
}
max= test( y= "A", z="score1", data = data)
Any help or suggestion would be appreciated!!