ref: Add Leading Zeros to the Elements of a Vector in R
参考代码:
# 数组
> seq(12)
[1] 1 2 3 4 5 6 7 8 9 10 11 12
# 格式化打印,自动补零
> sprintf("%02d", seq(12))
[1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12"
# 直接赋值,可以当成数组使用
> months = sprintf("%02d", seq(12))
> months
[1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12"
# for 循环,paste0 用来连接,相当于 Python 里面的加号,如果使用 paste,中间会多一个空格
> for(m in months) print(paste0("avg_tmins_", m))
[1] "avg_tmins_01"
[1] "avg_tmins_02"
[1] "avg_tmins_03"
[1] "avg_tmins_04"
[1] "avg_tmins_05"
[1] "avg_tmins_06"
[1] "avg_tmins_07"
[1] "avg_tmins_08"
[1] "avg_tmins_09"
[1] "avg_tmins_10"
[1] "avg_tmins_11"
[1] "avg_tmins_12"
标签:11,10,12,seq,857,tmins,操作,补零,avg From: https://www.cnblogs.com/alex-bn-lee/p/17545780.html