Comma Seperate A String

Original code: http://onertipaday.blogspot.com/2007/06/string-manipulation-insert-delim.html

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# Create a text string
cities <- c("sonomanapahealdsburgoakland")
# A vector with the locations of where we want each comma
comma.locations <- c(6,4,10,7)
# Place the commas into the string according to the locations specifics in comma.locations
paste(read.fwf(textConnection(cities), comma.locations, as.is = TRUE), collapse = ",")
[1] "sonoma,napa,healdsburg,oakland"