All answers posted so far include separate calls paste()and sprintf()instead of a single call sprintf().
The solution below uses the function CJ()(cross join) from the package data.table:
library(data.table)
CJ(1997:2006, 1:12)[, sprintf("%4i%02i", V1, V2)]
[1] "199701" "199702" "199703" "199704" "199705" "199706" "199707" "199708" "199709" "199710"
[11] "199711" "199712" "199801" "199802" "199803" "199804" "199805" "199806" "199807" "199808"
[21] "199809" "199810" "199811" "199812" "199901" "199902" "199903" "199904" "199905" ...
source
share