如何生成像a1,a2,a3这样的字符串...... 我试过了: 粘贴( “A”,1:3) [1]“a 1”“a 2”“a 3”
但我不想要空间
答案 0 :(得分:5)
除了paste
和paste0
之外,还有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"