Generate vector of a repeated string with incremental suffix number

烂漫一生 提交于 2020-01-02 03:24:30

问题


I would like to generate a vector based on repeating the string "FST" but with a number at the end which increments:

"Fst1" "Fst2" "Fst3" "Fst4" ... "Fst100"

回答1:


An alternative to paste is sprintf, which can be a bit more convenient if, for instance, you wanted to "pad" your digits with leading zeroes.

Here's an example:

sprintf("Fst%d", 1:10)     ## No padding
# [1] "Fst1"  "Fst2"  "Fst3"  "Fst4"  "Fst5"  
# [6] "Fst6"  "Fst7"  "Fst8"  "Fst9"  "Fst10"
sprintf("Fst%02d", 1:10)   ## Pads anything less than two digits with zero
# [1] "Fst01" "Fst02" "Fst03" "Fst04" "Fst05" 
# [6] "Fst06" "Fst07" "Fst08" "Fst09" "Fst10"

So, for your question, you would be looking at:

sprintf("Fst%d", 1:100) ## or sprintf("Fst%03d", 1:100)



回答2:


You can use the paste function to create a vector that combines a set character string with incremented numbers: paste0('Fst', 1:100)



来源:https://stackoverflow.com/questions/26234123/generate-vector-of-a-repeated-string-with-incremental-suffix-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!