Split name column with tidyr or regular expresion

I have this:

structure(list(Nombre = c(
  "Andrés PrietoA. Prieto", "Ángel JiménezÁ. Jiménez",
  "Alejandro PalopA. Palop", "Kevin SibilleK. Sibille", "Thomas CarriqueT. Carrique"
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame")))

I'm trying to split "Nombre" column into two: "Nombre_Completo" and "Nombre_abreviado". So I want, if name is "Andrés PrietoA. Prieto", in new df will be "Andrés Prieto" and "A. Prieto". I use

 mutate(Nombre_Abreviado = sub("^[^.]+\\s*(\\w)\\w+$", "\\1.", Nombre),
         Nombre_Completo = sub("\\s*\\w+\\s*(\\w+)$", " \\1", Nombre))

but results are not fine:

"Ángel JiménezÁ. Jiménez", "Alejandro PalopA. Palop", "Kevin SibilleK. Sibille", 
"Thomas CarriqueT. Carrique"),  Nombre_Abreviado = c("Andrés PrietoA. Prieto", "Ángel JiménezÁ. Jiménez", 
    "Alejandro PalopA. Palop", "Kevin SibilleK. Sibille", "Thomas CarriqueT. Carrique"
    ), Nombre_Completo = c("Andrés PrietoA. o", "Ángel JiménezÁ. z", 
    "Alejandro PalopA. p", "Kevin SibilleK. e", "Thomas CarriqueT. e"
    )), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))

Also with

separate(Nombre, into = c("Nombre_Completo", "Nombre_Abreviado"), sep = "\\.(?=\\s*\\w)")```

results are wrong:

``` rstructure(list(Número = c("1", "13", "-", "4", "17"), Nombre_Completo = c("Andrés PrietoA", 
"Ángel JiménezÁ", "Alejandro PalopA", "Kevin SibilleK", "Thomas CarriqueT"
)) row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))

I think all the extra variables dont help in this example ...

library(tidyverse)
(example_in <- structure(list(Nombre = c(
  "Andrés PrietoA. Prieto", "Ángel JiménezÁ. Jiménez",
  "Alejandro PalopA. Palop", "Kevin SibilleK. Sibille", "Thomas CarriqueT. Carrique"
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame")))


example_in |> mutate(
  step_1 = str_split_i(Nombre, fixed("."), 1),
  lastchar = substr(step_1, nchar(step_1), nchar(step_1)),
  Nombre_Completo = substr(step_1, 1, nchar(step_1) - 1),
  Nombre_Abreviado = paste0(
    lastchar,
    str_split_i(Nombre, fixed("."), 2)
  )
)

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.