compare columns

I have three columns ( home_team, away_team, winner).
home_team Away_ team winner
all Stars Bosses Bosses
Amigos Champions Amigos
Avenger Amigos Amigos

I want to create a fourth column named home_or_away that will compare home_team and winner. if home_team is winner then "home" and else "away"

In the future, it's very helpful if you can provide a reproducible example so the data is easy for someone to copy/paste into R. I did the work this time but in the future take a look here to learn how to create a reproducible example: FAQ: How to do a minimal reproducible example ( reprex ) for beginners

library(tidyverse)

tribble(
  ~home_team, ~Away_team, ~winner,
  'all Stars', 'Bosses', 'Bosses',
  'Amigos', 'Champions', 'Amigos',
  'Avenger', 'Amigos', 'Amigos'
) %>%
  mutate(
    home_or_away=if_else(winner==home_team, "home", "away")
  )
#> # A tibble: 3 x 4
#>   home_team Away_team winner home_or_away
#>   <chr>     <chr>     <chr>  <chr>       
#> 1 all Stars Bosses    Bosses away        
#> 2 Amigos    Champions Amigos home        
#> 3 Avenger   Amigos    Amigos away

Created on 2021-08-08 by the reprex package (v2.0.0)

Thank you for the answer.
I will do better next time and thanks for the link

This topic was automatically closed 21 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.