Home > OS >  How to replace whitespace by dot in a column of names?
How to replace whitespace by dot in a column of names?

Time:07-09

Here I have a column of names where the First and Last names are deliminated by a whitespace, how can I convert it to dot deliminated? Like: Wayne.Ribbon, Rio.Mansey...

df <- data.frame (name  = c("Wayne Ribbon", "Rio Mansey", "Alexandre Trakovski"),
                  age = c(38,54,29))

CodePudding user response:

Replace the whitespace by a period using sub(if you always have one first name and one family name; if there are more than two parts, use gsub):

library(dplyr)

df %>%
  mutate(name = sub(" ", ".", name))

#                  name age
# 1        Wayne.Ribbon  38
# 2          Rio.Mansey  54
# 3 Alexandre.Trakovski  29
  • Related