[R] New to R

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Feb 2 20:06:28 CET 2009


Joe Hughes wrote:
>
> #
> function(die_size, number_of_dice, number_of_rolls)
> {

you may want to replace

>     rolls <- array(0, dim=c(number_of_rolls, number_of_dice))
>     
>     for (i in 1:number_of_rolls)
>     {
>         rolls[i,] <- sample(die_size, number_of_dice, replace=TRUE)
>     }

with, e.g.

rolls = t(replicate(number_of_rolls, sample(die_size, number_of_dice,
replace=TRUE)))

to have it more r-ish

>     
>     return(rolls)
> }
>
> Any thoughts on this function?  

as soon as you start wrapping something like subset, lm, or the like
inside your functions, all hell breaks loose.  welcome to the r inferno,
that is. 


vQ




More information about the R-help mailing list