1

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.

  • 1
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with, including a sample of data and the code you're already working on – camille May 19 '21 at 18:05

2 Answers2

2
library(tibble)
library(dplyr)

df <- tibble(
  player = c("Ronaldo", "Messi", "Neymar", "Dibala"),
  Shooting = c(24,54,23,44),
  Defending = c(66,55,44,35)
)

df <- df %>%
  mutate(role = ifelse(Shooting > Defending, "Attacker", "Defender"))

df

Did you mean something like this?

Behnam Hedayat
  • 837
  • 4
  • 18
1

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')]
akrun
  • 874,273
  • 37
  • 540
  • 662