Alright my problem is, that I have a dataframe with a column with species names like this:
- Genus1 species1
- Genus1 species2
- Genus2 species3
And I simply want to cut the genus names like this:
- G. species1
- G. species2
- G. species3
My problem isn't the string manupilation. I'll just can't figure it out how to simply use my function that looks like this:
truncateGenusName <- function(x){
genus <- str_sub(x, 1, 1)
posOfSpace <- str_locate(x, " ")[1]
epitheton <- str_sub(x, posOfSpace, str_length(x))
paste0(genus, '. ', epitheton)
}
I was trying to call the function like this:
mutate(data, species = replace (species, TRUE, truncateGenusName(species)))
The problem with this call is that not a single string is passed to the function, but a list.
I guess my approach is way too complicated. Does maybe someone have a shortcut?