I'm working on R, I have a character data table like that :
Species<- c("lion", "tiger", "lion","tiger","donkey","lion","donkey")
Countries <- c("Tanzania", "Tanzania", "Kenya","Kenya","Italia","Niger","France")
df <- data.frame(Species, Countries)
| Species | Countries |
|---|---|
| lion | Tanzania |
| tiger | Tanzania |
| lion | Kenya |
| tiger | Kenya |
| donkey | Italia |
| lion | Niger |
| donkey | France |
With this table, I would like to have for each country of my dataframe the number of species in common for the countries 2 by 2, and have a final table like this one:
| Countries1 | Countries2 | number_common_species |
|---|---|---|
| Kenya | Tanzania | 2 |
| Italia | Tanzania | 0 |
| Italia | Kenya | 0 |
| Niger | Kenya | 1 |
| Niger | Tanzania | 1 |
| Italia | Niger | 0 |
| Italia | France | 1 |
| Tanzania | France | 0 |
| Kenya | France | 0 |
| Niger | France | 0 |
I have a really big dataset with a lot of species.
Does anyone know how I can do this?