Working in R, I have a dataframe (species_A) with species observed in several years and a lot of other information. I have a second dataframe (species_B) with species and the individual years they were sampled in.
I want to compare species_A with species_B so that species_A only contains species that are listed in species_B in the respective year.
This is how my data looks like:
Species_A:
ID__|__species _____________|___year
1___|__Diatoma vulgaris_______|___2005
2___|__Diatoma vulgaris_______|___2006
3___|__Nitzschia dissipata_____|___2006
4___|__Nitzschia palea________|___2007
The dataframe species_B is structured in the same way but does not contain all the rows of species_A.
This is the code I came up with if I would only want to compare if the species of species_A are contained in species_B. However, I want to group the species by year and then compare the dataframes.
species_A <- species_A[ species_A$species %in% species_B$species, ]
Can this possibly be done via dplyr?