Help to write a function with a for-Loop inside

Hi there,

I have 10 matrize. All Matrize have 10 col represent 10 Teams and 26 rows represent years. In every field is a score for a given year and Team. Now I want to rank very Team in every row. For example 2007: Team1 = 5, Team2=2, Team3 =1

I tried follow code....

leaderboard <- function(x){
Leaderboard_Results <- matrix(nrow = 26, ncol = 10)
for (i in c(1:26)){
Leaderboard_Results[i,] <- rank(x[i,], ties.method="average")
}
colnames(Leaderboard_Results) <- c("Team1", "Team2", "Team3", "Team4", "Team5, "Team6", "Team7", "Team8", "Team9", "Team10")
rownames(Leaderboard_Results) <- c(1993:2018)
}

Test <- leaderboard(Matrix1)

Without the function It works. Where is my mistake?

The main problem is that your function does not return the Leaderboard_Results matrix. Try

leaderboard <- function(x){
  Leaderboard_Results <- matrix(nrow = 26, ncol = 10)
  for (i in c(1:26)){
    Leaderboard_Results[i,] <- rank(x[i,], ties.method="average")
  }
  colnames(Leaderboard_Results) <- c("Team1", "Team2", "Team3", "Team4", "Team5", 
                                     "Team6", "Team7", "Team8", "Team9", "Team10")
rownames(Leaderboard_Results) <- c(1993:2018)
Leaderboard_Results
}

There was also a missing double quote in the code you posted but I expect that was a typo.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.