如何在R中生成一系列字符串,如a1,a2,a3 a4 ....

时间:2014-02-27 16:56:12

标签: string r character paste

如何生成像a1,a2,a3这样的字符串...... 我试过了:     粘贴( “A”,1:3)     [1]“a 1”“a 2”“a 3”

但我不想要空间

3 个答案:

答案 0 :(得分:5)

除了pastepaste0之外,还有sprintf可以更好地控制格式:

> sprintf("a%d", 1:5)
[1] "a1" "a2" "a3" "a4" "a5"
> sprintf("a%d", 1:15)
 [1] "a1"  "a2"  "a3"  "a4"  "a5"  "a6"  "a7"  "a8"  "a9"  "a10" "a11" "a12"
[13] "a13" "a14" "a15"
> sprintf("a%02d", 1:15)
 [1] "a01" "a02" "a03" "a04" "a05" "a06" "a07" "a08" "a09" "a10" "a11" "a12"
[13] "a13" "a14" "a15"

答案 1 :(得分:3)

使用paste0(...)paste(..., sep = "")paste的默认值是插入空格。

答案 2 :(得分:1)

> paste0("a", 1:3)
## [1] "a1" "a2" "a3"