[R] Creating data using multiple for loops

William Dunlap wdun|@p @end|ng |rom t|bco@com
Mon Aug 19 20:29:36 CEST 2019


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 using gmail.com> wrote:

> id <- do.call(paste0,expand.grid(0:9, 1:3, 1:5))
>
> Comment: If you use R much, you'll do much better using R language
> constructs than trying to apply those from other languages (Java perhaps?).
> I realize this can be difficult, especially if you are experienced in the
> another language (or languages), but it's worth the effort.
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Sun, Aug 18, 2019 at 11:58 AM <g.eastham.gilbert using gmail.com> wrote:
>
> > I would like to create pseudo identification numbers in the format of
> last
> > four of a social security number (0000 to 9999), month of birth (01 to
> 12),
> > and day of birth (01-28). The IDs can be character.
> >
> > I have gotten this far:
> >
> > for (ssn in 0:9){
> >      for (month in 1:3){
> >           for (day in 1:5){
> >                       }
> >                       id <-paste(ssn, month, day, sep="")
> >             }
> > }
> >
> > limiting each value above for demonstration purposes. I cannot figure out
> > how to store the created IDs. I know I have to create a container, but I
> > don't know, among other things, how to index the container.  Any help is
> > appreciated. TIA
> >
> > -Greg
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list