Also you don't need return since it automatically returns the value produced by the last statement that it executes.
On Mon, Feb 2, 2009 at 3:44 PM, Greg Snow <greg.s...@imail.org> wrote: > It is not transposing (it just looks that way). The first result is a vector > which is 1 dimensional, but is neither a row or a column. The printed > version of it looks like a row, because that is a more compact > representation. If you sample enough points you will see it wrap around and > be represented as several rows. If it printed as a single column, then the > first values would scroll off the screen with only a moderate number of > values. > > The replicate function then takes these vectors and combines them into a > matrix and just happens to use each vector as a column of the new matrix, > this is standard, matrices by default are filled by column, look at the > output of as.matrix( sample( 6, 4, replace=TRUE ) ) and you will see your > vector converted to a matrix of 1 column. It could have been done the other > way, but way back the decision was made to do it this way and there are > probably a lot of things that would break if it were changed now, so we get > to live with it. A single call to 't' is not too much effort to get what we > expect. > > So in short, a vector is neither a column or a row, but prints as a row for > practical reasons, and is converted to a column by default if made into a > matrix. > > Hope this helps, > > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.s...@imail.org > 801.408.8111 > > >> -----Original Message----- >> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- >> project.org] On Behalf Of Joe Hughes >> Sent: Monday, February 02, 2009 1:09 PM >> To: R help >> Subject: Re: [R] New to R >> >> 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 >> >> ______________________________________________ >> 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. > > ______________________________________________ > 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. > ______________________________________________ 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.