Search code examples
rdataframecategorical-data

Creating a new categorical column in a data frame using numerical values from that same data frame?


I am trying to assign each observation within my data frame a categorical value based on the highest number from 4 different numerically valued columns.

I am working with a list of all FIFA soccer players and if their highest rating is their Shooting stat for instance, then they are an Attacker, if highest in Defending, then Defender, you get the point.


Solution

  • We could use case_when

    library(dplyr)
    df %>%
        mutate(role = Shooting > Defending ~ "Attacker", TRUE ~ "Defender"))
    

    Or another option is max.col

    df$role <- c("Attacker", "Defender")[max.col(df[c("Shooting", 
            "Defending")], 'first')]