Topher Topher - 1 year ago 73
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 Source

Solution by HubertL:

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

Nicer solution:

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download