Skip to content
Prev 380769 / 398498 Next

Creating data using multiple for loops

do.call(paste0,expand.grid(0:1000, 1:12, 1:30)) takes care of storing all
the values, but note that paste() doesn't put leading zeroes in front of
small numbers so this maps lots of  ssn/month/day combos to the the same
id.  sprintf() can take care of that:
id <- with(expand.grid(ssn=0:1000, month=1:12, day=1:30),
sprintf("%04d%02d%02d", ssn, month, day))

You probably should define a function to map vectors of ssn, month,  and
day to a vector of ids (it can also check for inappropriate inputs), check
that it works, and use it instead of repeating the sprintf() or paste0()
code.

Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Sun, Aug 18, 2019 at 12:18 PM Bert Gunter <bgunter.4567 at gmail.com> wrote: