vendredi 6 novembre 2020

If else loop in R that adds a value every time it returns true

So I am trying to create a bar graph that represents the number of times an NFL team has won the superbowl. I want it to add 1 for every time the value returns TRUE so that the bar graph shows the teams who have won the most times.

nfl$repeatwinners <- duplicated(nfl$Winner) | duplicated(nfl$Winner, fromLast = TRUE)

fill_colors <- c()
for ( i in 1:length(nfl$repeatwinners) ) {
    if (nfl$repeatwinners[i] == TRUE) {
        fill_colors <- c(fill_colors, "blue")} 
    else {
        fill_colors <- c(fill_colors, "grey")}}

barplot(nfl$repeatwinners, names.arg = nfl$SB, col = fill_colors, border = NA, xlab = "Superbowl")

Aucun commentaire:

Enregistrer un commentaire