[R] Creating data using multiple for loops

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Sun Aug 18 23:46:15 CEST 2019


Hi Greg,
One problem is that you have misplaced the closing brace in the third
loop. It should follow the assignment statement. Because you used
loops rather than Bert's suggestion, perhaps you are trying to order
the values assigned. In your example, the ordering will be ssn, then
month of birth, then day of birth. Occasionally people resort to an
explicit calculation for the index:

id<-vector("character",10*3*5)
for (ssn in 0:9){
     for (month in 1:3){
          for (day in 1:5){
                      id[day+(month-1)*5+ssn*15] <-paste0(ssn, month, day)
          }
     }
}

This would order the values in the opposite precedence. Also, you may
not want to create well over 3 million values as in your initial
specification, in which case a different strategy using "sample" would
be appropriate.

Jim

On Mon, Aug 19, 2019 at 4: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.



More information about the R-help mailing list