Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sapply paste before at beginning of string

Tags:

r

sapply

paste

So I have a vector

lizt <- c("a","b","c")

> lizt
[1] "a" "b" "c"

and I can use sapply to paste characters after each element

lizt2 <- sapply(lizt,paste0, "$", USE.NAMES=F)

lizt2
[1] "a$" "b$" "c$"

now, how do I use a similar function to paste characters before each element, so I get

lizt3
[1] "^a$" "^b$" "^c$"
like image 385
dmvianna Avatar asked Sep 11 '12 02:09

dmvianna


1 Answers

paste and paste0 are vectorized, so you don't need sapply

paste0('^', lizt, '$')
## [1] "^a$" "^b$" "^c$"
like image 145
mnel Avatar answered Nov 08 '22 06:11

mnel