[R] New to R

Joe Hughes joe.hughes at earthlink.net
Mon Feb 2 21:09:10 CET 2009


All,

	Thanks for taking the time to reply.  I understand a bit more about R  
and the R way then I did before.    The final function looks like this:

##############################################################################
#
# Input:
#   die_size - 4, 6, 8, 10, 20
#   number_of_dice - How many dice to roll
#   number_of_rolls - How many times to roll the dice
#
# Output:
#    The array holding the results of the rolls
#
##############################################################################
#
function(die_size, number_of_dice, number_of_rolls=1)
{
	return(t(replicate(number_of_rolls, sample(die_size, number_of_dice,  
replace=TRUE))))
}

Before I take a look at the teaching demos, I have one question left.

Here is a sequence of commands and the output

 > sample(6, 4, replace=TRUE)
[1] 3  4  5  4
 > replicate(7, sample(6, 4, replace=TRUE))
      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    3    3    6    4    5    6    6
[2,]    4    4    6    5    5    1    6
[3,]    5    1    4    5    6    5    6
[4,]    4    6    3    1    1    2    2

Why does replicate transpose the vector before assigning it to the  
array?  The way I would output it would be this

      [,1] [,2] [,3] [,4]
[1,]    3    4    5    4
[2,]    3    4    1    6
[3,]    6    6    4    3
[4,]    4    5    5    1
[5,]    5    5    6    1
[6,]    6    1    5    2
[7,]    6    6    6    2

Thanks,
Joe




More information about the R-help mailing list