sprintf 在没有 gsub 的情况下使用 [英] sprintf use without gsub
本文介绍了sprintf 在没有 gsub 的情况下使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于这行代码,只使用 sprintf
而没有 gsub
的更优雅的方法是什么?
what is the more elegant way of just using sprintf
without the gsub
for this line of code?
gsub(" ","0",sprintf("%2.d", 0:15))
[1] "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15"
即我想要个位数前面的零...作为字符输出.
i.e. I want the zeros infront of the single digit numbers...as output as characters.
推荐答案
在格式字符串中使用 0
来填充前导零而不是空格:sprintf("%02d",0:15)
Use a 0
in the format string to pad with leading zeros instead of spaces: sprintf("%02d", 0:15)
这篇关于sprintf 在没有 gsub 的情况下使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文