0

I have a data frame with two columns below:

HomeTeam     AwayTeam
Zimbabwe     Kenya
Netherlands  Zimbabwe
Kenya        Amsterdam

I want to create a column Team from both these column but it shouldn't repeat the name of the team. How do I go about it?

LuLuGaGa
  • 13,089
  • 6
  • 49
  • 57
IAwa
  • 5
  • 3

2 Answers2

1

This is a little bit of a guess since your question is not perfectly clear, but I think union() is what you're looking for ...

Make up an example:

dd <- read.table(header = TRUE, text = "
HomeTeam     AwayTeam
Zimbabwe     Kenya
Netherlands  Zimbabwe
Kenya        Amsterdam
")

Construct a data frame that contains a single column with the (non-repeated) team names from both columns:

result <- data.frame(Team = with(dd, union(HomeTeam, AwayTeam)))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Hi. Thanks for your reply. This was just an example of what the data looked like.I have a data base called EPL so do I mention "EPL" in 'text ='? – IAwa Oct 22 '22 at 18:32
  • My data frame is called `dd`; if yours is called `EPL` then substitute that for `dd` in the last line of code shown (the rest is just there to set up an example). – Ben Bolker Oct 22 '22 at 18:34
  • 2
    If this solves your problem you are encouraged to click the check-mark to accept it (and upvote, if you want) – Ben Bolker Oct 22 '22 at 19:15
1

Here is a tidyverse option:

library(dplyr)
library(tidyr)

df %>%
  pivot_longer(everything(), 
               values_to = "Team") %>% 
  distinct(Team)
  Team       
  <chr>      
1 Zimbabwe   
2 Kenya      
3 Netherlands
4 Amsterdam  
TarJae
  • 72,363
  • 6
  • 19
  • 66