Reputation: 797
I would like to add whitespace
between a .
and number
.
My exampe as below
df <- data.frame(name1 = c("Acranthera sp.01", "Aglaia spectabilis (Miq.) S.S.Jain & Bennet", "Alyxia sp.11"))
df
name1
1 Acranthera sp.01
2 Aglaia spectabilis (Miq.) S.S.Jain & Bennet
3 Alyxia sp.11
My desired output
name1
1 Acranthera sp. 01
2 Aglaia spectabilis (Miq.) S.S.Jain & Bennet
3 Alyxia sp. 11
So far, I only know to use str_pad
to add the space. Any sugesstions for this with tidyverse
and stringr
?
Upvotes: 0
Views: 360
Reputation: 10786
mutate(
df,
name1 = str_replace(
name1,
"\\.(\\d)", # Match a literal . followed by a digit (capturing group 1)
". \\1" # Replace by a ., space and the content of the first capturing group (the digit following the .)
)
)
Upvotes: 1