Home > Software design >  finding second space and remove in R
finding second space and remove in R

Time:10-05

I would like to remove second space of several names (after sp.) in R using tidyverse

My example:

df <- data.frame(x = c("Araceae sp. 22", "Arecaceae sp. 02"))

My desired output

x
Araceae sp.22
Arecaceae sp.02

Any suggestions for me, please?

CodePudding user response:

We may use sub to capture the one or more characters that are not a spaces followed by space (\\s ) and another set of characters not a space and replace with the backreference of the captured group

df$x <- sub("^(\\S \\s \\S )\\s ", "\\1", df$x)
df$x
[1] "Araceae sp.22"   "Arecaceae sp.02"

Or we can use str_replace

library(dplyr)
library(stringr)
df %>%
    mutate(x = str_replace(x, "^(\\S \\s \\S )\\s ", "\\1"))
  • Related