Hi, I have a dataframe like the one below. I want to make a new column called "Species". I then want to populate that column with either "species1" or "species2", depending on the end of the sample ID (in the ID column). I have an example below of what I have tried, trying to use mutate, ends_with, and if_else, but I haven't figured out how to make it work yet.
library(dplyr)
library(tidyr)
library(stringr)
#make dataframe
ID<-c("ind1_species2","ind2_species2","ind1_species1","ind2_species2")
Year<-c(1999,1999,2000,2000)
df<-data.frame(ID,Year)
colnames(df)
#example of failed code
a<- df %>%
mutate(Species=ifelse(ends_with("species2")),"species2","species1")