Search code examples
rmergemultiple-columnsmutate

Combine two columns in one in R without duplication


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?


Solution

  • 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)))