Hi:

Here's the problem I had with the OP's function:

On Wed, Aug 18, 2010 at 4:26 AM, David Winsemius <dwinsemius@comcast.net>wrote:

>
> On Aug 18, 2010, at 5:18 AM, Philip Wong wrote:
>
>
>> hello people,
>> I want to make a biased dice using the sample() function and print out the
>> results after n number of runs, I've successfully generated the dice using
>> the following command:
>>
>


> mydie2<-function(n=1000,y=NULL,...){
>> for(i in 1:n){
>>
> # simulate 1000 die tosses using unequal probabilities (OK, no problem
there)

> x<-sample(1:6,n,replace=TRUE,prob=c(1,1,2,3,2,1)/10)
>>
> # REPLACE the simulations with 1000 random draws from a U(0, 1)  (!!!!)

> x=runif(n)
>>
> # Now divide up the 1000 uniform random numbers into groups 1:6,
# weighted by the probabilities used to get the first set of simulations

> if(x<=1/10){y[i]=1}
>> else if(x<=2/10){y[i]=2}
>> else if(x<=4/10){y[i]=3}
>> else if(x<=7/10){y[i]=4}
>> else if(x<=9/10){y[i]=5}
>> else{y[i]=6}
>> }
>>
>
# One would think that the idea is to compare the frequencies from the
simulation
# of coin tosses to that of the U(0, 1) random numbers grouped by
probability,
# but that comparison is not done below because half the information was
# overwritten...unless the first set of simulations from sample() is a red
herring.

bar<-barplot(table(y))
>> table<-table(y)
>> return(list(bar,table))
>> }
>>
>> mydie2()
>>
>
I would tend to agree with David that this smells like homework.

Suggestions:
(1) Create a object for your uniform random draws with a different name.
(2) Learn about the cut() function:   ?cut
(3) Learn about vectorization in R; it's much more efficient, especially in
basic programming. A 'whole object view' gets you a lot further in R
programming than does the element-by-element view common in other
programming languages.

HTH,
Dennis

>
>>
>> but I also want to try to create the same dice stimulation using ifelse()
>>
>
> When problems are posed with a requirement to use a particular function, it
> makes me think there is a homework assignment underlying the request, so I
> will offer a hint rather than making an effort at a solution to your full
> problem. You can use nested ifelse's:
>
> > set.seed(123); x=runif(20); y<- vector("numeric", length(x))
> > y <- ifelse( x<=1/10, 1, ifelse( x <= 2/10, 2, ifelse (x <= 3/10 , 3,
> 4)))
> > x; y
>
>  [1] 0.28757752 0.78830514 0.40897692 0.88301740 0.94046728 0.04555650
> 0.52810549 0.89241904
>  [9] 0.55143501 0.45661474 0.95683335 0.45333416 0.67757064 0.57263340
> 0.10292468 0.89982497
> [17] 0.24608773 0.04205953 0.32792072 0.95450365
>  [1] 3 4 4 4 4 1 4 4 4 4 4 4 4 4 2 4 3 1 4 4
>
>
>
>  statement, but all hell break loose when I attempt to use the command
>> below
>> (the barplot only shows the results to be 6), can anyone tell me what went
>> wrong please:
>> mydie2<-function(n=1000,y=NULL,...){
>> for(i in 1:n){
>> x<-sample(1:6,n,replace=TRUE,prob=c(1,1,2,3,2,1)/10)
>> x=runif(n)
>> ifelse((x<=1/10),{y[i]<-1},
>> ifelse((x<=2/10),{y[i]<-2},
>> ifelse((x<=4/10),{y[i]<-3},
>> ifelse((x<=7/10),{y[i]<-4},
>> ifelse((x<=9/10),{y[i]<-5},{y[i]<-6})))))
>>
>> }
>> bar<-barplot(table(y))
>> table<-table(y)
>> return(list(bar,table))
>> }
>>
>>
>> mydie2()
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/ifelse-command-tp2329538p2329538.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help@r-project.org mailing list
>> 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.
>>
>
> David Winsemius, MD
> West Hartford, CT
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> 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]]

