I am trying to replace the space between single letters (initials of names) with a period. There should also be a trailing period after the final letter.
For example:
"h a g wells j g verne" becomes "h.a.g. wells j.g. verne"
and
"h a g wells j g" becomes "h.a.g. wells j.g."
This question Removing spaces between single letters, has got it most of the way but I am struggling to get the trailing period. Could I have some help please.
My efforts
x <- "h a g wells j g verne"
y <- "h a g wells j g"
# Almost but need trailing period as well
temp <- gsub("(?<=(?<!\\w)\\w) (?=\\w(?!\\w))", ".", x , perl=TRUE)
temp
# [1] "h.a.g wells j.g verne"
# Trying for the trailing periods
# messes first by matching first letter but okay in second
gsub("(\\.\\w)", "\\1.", temp)
# [1] "h.a..g. wells j.g. verne"
# messes first by matching first letter but okay in second
gsub("(\\w\\.\\w)", "\\1.", temp)
# [1] "h.a..g wells j.g. verne"
# messes first by matching first letter but okay in second
gsub("(?<=\\.\\w)", ".", temp, perl=TRUE)
# [1] "h.a..g. wells j.g. verne"
# Success
temp2 <- gsub("(\\.\\w)\\s", "\\1. ", temp)
temp2
# [1] "h.a.g. wells j.g. verne"
# ... but not for y which misses the second trailing period
temp <- gsub("(?<=(?<!\\w)\\w) (?=\\w(?!\\w))", ".", y , perl=TRUE)
gsub("(\\.\\w)\\s", "\\1. ", temp)
# [1] "h.a.g. wells j.g"