Topher Topher - 14 days ago 6
R Question

Insert spaces to characters without replacement in R

I have a character vector as follows:

x = c("word", "another", "final", "more")


I would like to include spaces before and/or after each word.

y = c(" word ", " another ", " final ", " more ")


Any help would be appreciated.

Answer

Solution by HubertL:

y <- paste0(" ", x, " ")

Nicer solution:

y <- paste("", x, "")

This is assuming you want spaces in either sides of every word.