Perhaps different people find different concepts the most challenging, but I
find looking at the output of expand.grid quite informative... do try it out.
The do.call function seems to be the more obscure function here, but Bert's code
id <- do.call( paste0, expand.grid(0:9,1:3,1:5) )
is equiva
>From section 9.2.2 (on looping) in "An Introduction to R":
"*Warning*: for() loops are used in R code much less often than in compiled
languages. Code that takes a ‘whole object’ view is likely to be both
clearer and faster in R."
Web searching on "for loops in R" and similar will give you furth
Hi Greg,
I replied because I thought the name of the "expand.grid" function can
be puzzling. While "expand.grid" is a very elegant and useful
function, it is much easier to see what is happening with explicit
loops rather than loops buried deep inside "expand.grid". Also note
Bill's comment about p
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=
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,
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),
I would like to create pseudo identification numbers in the format of last
four of a social security number ( to ), 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){
7 matches
Mail list logo