Topher Topher - 4 months ago 32
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.


Solution by HubertL:

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

Nicer solution:

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

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