Why not d <- ifelse( d1 > d2, d1, d2 )
or d <- pmax( d1, d2 ) ? Apply operations may seem "beautiful", but I think the speed and simplicity of vectorized operations are truly beautiful. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnew...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Jaap van Wyk <j.van...@ru.ac.za> wrote: Hallo I am teaching a very elementary intro course about R, and want to show the students how to find the distribution of the maximum of rolling two balanced die. Is there perhaps a more elegant way to do this, other than the way I am using below? (I would like to show them two ways - the one shown here, and perhaps and improved/elegant approach (showing off the "beauty" of R. My code is as follows: R> n <- 20000 R> d1 <- sample(1:6, n, replace=TRUE) R> d2 <- sample(1:6, n, replace=TRUE) R> d <- apply(matrix(c(d1,d2), nrow=n, byrow=TRUE), 1, max) R> round(table(d)/n, 3) d 1 2 3 4 5 6 0.030 0.084 0.137 0.195 0.246 0.308 _____________________________________________ 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]] ______________________________________________ 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.