Вопрос

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$"
Это было полезно?

Решение

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

paste0('^', lizt, '$')
## [1] "^a$" "^b$" "^c$"

Другие советы

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$"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top