combining mutate, ends_with, and if_else

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.


#make dataframe

#example of failed code
a<- df %>%

Does it do what you want?

#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union

df <- data.frame(ID = c("ind1_species2", "ind2_species2", "ind1_species1", "ind2_species2"),
                 Year = c(1999, 1999, 2000, 2000))
df %>%
    mutate(Species = str_extract(string = df[["ID"]],
                                 pattern = "(?<=_)(species[12])$"))
#>              ID Year  Species
#> 1 ind1_species2 1999 species2
#> 2 ind2_species2 1999 species2
#> 3 ind1_species1 2000 species1
#> 4 ind2_species2 2000 species2

Created on 2021-03-18 by the reprex package (v1.0.0)

1 Like

That works great. Thanks a lot for the quick response!

Hi again,

I'm confused what the "[12]" means here. Just wondering if there is a quick explanation you could share?


Sure, no problem.

This regular expression will match either species1 or species2 at the end of a word followed by an underscore (_). What [12] means is either 1 or 2, and no other character.

You may find this site helpful to understand different regex you find online:

1 Like

Thanks so much, that is really helpful!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.