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){ x<-sample(1:6,n,replace=TRUE,prob=c(1,1,2,3,2,1)/10) x=runif(n) 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} } bar<-barplot(table(y)) table<-table(y) return(list(bar,table)) } mydie2() but I also want to try to create the same dice stimulation using ifelse() 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.
Hi Philip, Why do you want to use ifelse? It is not an alternative to an if then else structure, but a way of iterating over a vector with a statement, returning one value it is true, another if false. An example: x = runif(100) y = ifelse(x > 0.5, "Larger", "Smaller") x y cheers, Paul On 08/18/2010 11: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){ > x<-sample(1:6,n,replace=TRUE,prob=c(1,1,2,3,2,1)/10) > x=runif(n) > 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} > } > bar<-barplot(table(y)) > table<-table(y) > return(list(bar,table)) > } > > mydie2() > > > but I also want to try to create the same dice stimulation using ifelse() > 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() >-- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +3130 253 5773 http://intamap.geo.uu.nl/~paul http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770
Dear Philip, Trying to use 5 nested ifelse() statements will not be the same as a series of if statements, and will also be quite messy. Also, consider these lines of your code: x <- sample(1:6,n,replace=TRUE,prob=c(1,1,2,3,2,1)/10) x = runif(n) First you assign n samples of 1:6 to 'x', then you immediately overwrite 'x' with 1000 random samples from the uniform distribution. If you try your function after removing the x <- sample.... line, you will find it does nothing (well, it does make the process take somewhat longer). On another note, after you 'succeeded' with the if statements, did you look at the 50+ warnings? These should have cued you in that something might have been going wrong. Look at the following: set.seed(1) x <- runif(3) y <- x # assign a copy of 'x' to 'y' x ifelse(1 < 2, {x[1] = 999}, {x[1] = 888} ) ifelse(c(1, 3) < 2, {x[2] = 999}, {x[2] = 888} ) ifelse(c(3, 1) < 2, {x[3] = 999}, {x[3] = 888} ) if(1 < 2) {y[1] = 999} else {y[1] = 888} if(c(1, 3) < 2) {y[2] = 999} else {y[2] = 888} if(c(3, 1) < 2) {y[3] = 999} else {y[3] = 888} # Note the warnings() from using if statements # and that x and y are not equal x y I think you are looking for: dat <- sample(1:6, 1000, prob = c(1, 1, 2, 3, 2, 1)/10, replace = TRUE) barplot(table(dat)) If you really want to see the results after running that 1000 times, perhaps.... datbig <- sample(1:6, 1000*1000, prob = c(1, 1, 2, 3, 2, 1)/10, replace = TRUE) barplot(table(datbig)) HTH, Josh On Wed, Aug 18, 2010 at 2:18 AM, Philip Wong <tombfighter at mysinamail.com> 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){ > x<-sample(1:6,n,replace=TRUE,prob=c(1,1,2,3,2,1)/10) > x=runif(n) > 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} > } > bar<-barplot(table(y)) > table<-table(y) > return(list(bar,table)) > } > > mydie2() > > > but I also want to try to create the same dice stimulation using ifelse() > 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 at 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
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){ > x<-sample(1:6,n,replace=TRUE,prob=c(1,1,2,3,2,1)/10) > x=runif(n) > 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} > } > bar<-barplot(table(y)) > table<-table(y) > return(list(bar,table)) > } > > mydie2() > > > 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 at 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
Or try: datbig<-numeric(1000) for (i in 1:1000){ datbig[i] <- mean(sample(1:6, 1000, prob = c(1, 1, 2, 3, 2, 1)/10, replace = TRUE)) hist(datbig,breaks="FD") -- View this message in context: http://r.789695.n4.nabble.com/ifelse-command-tp2329538p2329711.html Sent from the R help mailing list archive at Nabble.com.
sorry missed an "}" datbig<-numeric(1000) for (i in 1:1000){ datbig[i] <- mean(sample(1:6, 1000, prob = c(1, 1, 2, 3, 2, 1)/10, replace = TRUE)) hist(datbig,breaks="FD") } -- View this message in context: http://r.789695.n4.nabble.com/ifelse-command-tp2329538p2329717.html Sent from the R help mailing list archive at Nabble.com.