How to paste a string on each element of a vector of strings using apply in R?

pedrosaurio picture pedrosaurio · Aug 8, 2011 · Viewed 61.4k times · Source

I have a vector of strings.

d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun")

for which I want to paste the string "day" on each element of the vector in a way similar to this.

week <- apply(d, "day", paste, sep='')

Answer

Dirk Eddelbuettel picture Dirk Eddelbuettel · Aug 8, 2011

No need for apply(), just use paste():

R> d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun")
R> week <- paste(d, "day", sep="")
R> week
[1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  
[4] "Friday"    "Saturday"  "Sunday"   
R>