Home > OS >  Combine two columns in one in R without duplication
Combine two columns in one in R without duplication

Time:10-23

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?

CodePudding user response:

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

CodePudding user response:

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