Reputation: 27
I have a dataframe in R
that looks like this:
genus | species |
---|---|
Vulgatibacter | Vulgatibacter sp. |
NA | Planctomyces |
Holophaga | Geothrix sp. |
And I want to delete values from column species if the value from genus does not contain in species. I want to have this:
genus | species |
---|---|
Vulgatibacter | Vulgatibacter sp. |
NA | Planctomyces |
Holophaga | NA |
transform(.,Species= ifelse(Genus %in% Species, Species, NA))
does not work.
Upvotes: 0
Views: 58
Reputation: 3194
df |>
mutate(species = case_when(mapply(grepl, sprintf("^%s", genus), species) ~ species,
is.na(genus) ~ species,
TRUE ~ NA_character_))
#> # A tibble: 3 × 2
#> genus species
#> <chr> <chr>
#> 1 Vulgatibacter Vulgatibacter sp.
#> 2 NA Planctomyces
#> 3 Holophaga NA
Upvotes: 1