0

假设我有一个向量:

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

我该怎么做才能将其转换为:

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

我尝试了几次粘贴,但无法完全得到我想要的。

4

2 回答 2

8

paste被矢量化:

> tt <- c("test", "this", "function")
> paste(tt,"AS",tt)
[1] "test AS test"         "this AS this"         "function AS function"
于 2013-10-23T23:01:30.157 回答
1

你会想要使用sapply

> sapply(tt, function(X) paste0(X, " AS ", X))
                  test                   this               function 
        "test AS test"         "this AS this" "function AS function" 
于 2013-10-23T23:00:35.190 回答