user18948933
user18948933

Reputation:

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

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))

Upvotes: 0

Views: 604

Answers (1)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21432

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

Upvotes: 3

Related Questions