Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以我有一个向量
lizt <- c("a","b","c") > lizt [1] "a" "b" "c"
我可以使用 sapply在每个元素之后粘贴字符
lizt2 <- sapply(lizt,paste0, "$", USE.NAMES=F) lizt2 [1] "a$" "b$" "c$"
现在,我如何使用类似的功能在每个元素之前粘贴字符,所以我得到
lizt3 [1] "^a$" "^b$" "^c$"
paste并且paste0是矢量化的,所以你不需要sapply
paste
paste0
sapply
paste0('^', lizt, '$') ## [1] "^a$" "^b$" "^c$"
正如mnel 所示,你不需要在sapply这里使用,但如果你想使用,你可以创建自己的自定义函数来使用,sapply如下所示:
> sapply(lizt, function(x) paste0("^", x, "$"), USE.NAMES=FALSE) [1] "^a$" "^b$" "^c$"