Suppose we have a string of last names like the following:
nom <- c("Perez Conchito", "Juanin Juanharry", "Von Bola")
I have designed a small function to extract each part of the string and then gather them into a data.frame with two columns.
extract_apellidos <- function(x) {
first_split <- strsplit(x, " ")
split_un <- unlist(first_split)
primer_ap <- split_un[c(TRUE, FALSE)] #Nos da el primer apellido
segundo_ap <- split_un[c(FALSE,TRUE)] #Nos da el segundo apellido
data.frame(Primer_ap=primer_ap, Segundo_ap=segundo_ap)
}
extract_apellidos(nom)
Primer_ap Segundo_ap
1 Perez Conchito
2 Juanin Juanharry
3 Von Bola
As you can see, it works correctly. However, I would like to know if it is possible to optimize it using regular functions since I suspect that this would allow me to reduce the number of steps used. I thank you in advance for any guidance on this.