Reputation: 15718
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$"
Upvotes: 8
Views: 7957
Reputation: 49820
As mnel showed, you do not need to use sapply
here, but if you want to anyway, you can create your own custom function to use with sapply
like this:
> sapply(lizt, function(x) paste0("^", x, "$"), USE.NAMES=FALSE)
[1] "^a$" "^b$" "^c$"
Upvotes: 5
Reputation: 115390
paste
and paste0
are vectorized, so you don't need sapply
paste0('^', lizt, '$')
## [1] "^a$" "^b$" "^c$"
Upvotes: 16