重复矢量本身

时间:2013-10-23 22:58:38

标签: r

说我有一个矢量:

tt <- c("test", "this" "function")

我可以做些什么来将其转换为:

>tt "test AS test", "this AS this", "function AS function"

我尝试了几次粘贴迭代但却无法得到我想要的东西。

2 个答案:

答案 0 :(得分:8)

paste已向量化:

> tt <- c("test", "this", "function")
> paste(tt,"AS",tt)
[1] "test AS test"         "this AS this"         "function AS function"

答案 1 :(得分:1)

您需要使用sapply

> sapply(tt, function(X) paste0(X, " AS ", X))
                  test                   this               function 
        "test AS test"         "this AS this" "function AS function"