Reputation: 15
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?
Upvotes: 1
Views: 730
Reputation: 78927
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
Upvotes: 1
Reputation: 226172
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)))
Upvotes: 2