Manual under the conditions for verbatim copying, provided that theĮntire resulting derived work is distributed under the terms of a
Permission is granted to copy and distribute modified versions of this Manual provided the copyright notice and this permission notice are Permission is granted to make and distribute verbatim copies of this This manual provides information on data types, programming elements,Ĭopyright © 1992 W. (linear and nonlinear modelling, statistical tests, time seriesĪnalysis, classification, clustering. It provides a wide variety of statistical and graphical techniques System, which was developed at Bell Laboratories by John Chambers et al. This is an introduction to R (“GNU S”), a language and environment for If you subset this table by rolls$Var1, you will get a vector of probabilities perfectly keyed to the values of Var1: rolls $Var1 First, we can look up the probabilities of rolling the values in Var1. Let me suggest a three-step process for calculating these probabilities in R. So the probability that we roll a (1, 1) will be equal to the probability that we roll a one on the first die, 1/8, times the probability that we roll a one on the second die, 1/8:Īnd the probability that we roll a (1, 2) will be: The probability that n independent, random events all occur is equal to the product of the probabilities that each random event occurs. You can calculate this with a basic rule of probability:
Next, you must determine the probability that each combination appears. As a result, each element of value will refer to the elements of Var1 and Var2 that appear in the same row. R will match up the elements in each vector before adding them together. This will be the sum of the two dice, which you can calculate using R’s element-wise execution: rolls $value <- rolls $Var1 + rolls $Var2 You can determine the value of each roll once you’ve made your list of outcomes. Each combination will contain exactly one element from each vector. id will always return a data frame that contains each possible combination of n elements from the n vectors. For example, you could list every combination of rolling three dice with id(die, die, die) and every combination of rolling four dice with id(die, die, die, die), and so on. You can use id with more than two vectors if you like. This will capture all 36 possible combinations of values: rolls To do so, run id on two copies of die: rolls <- id(die, die)Įid will return a data frame that contains every way to pair an element from the first die vector with an element from the second die vector. For example, you can list every combination of two dice. The id function in R provides a quick way to write out every combination of the elements in n vectors. Listing out these combinations can be tedious, but R has a function that can help. Or, you may roll (1, 2), one on the first die and two on the second. For example, you might roll (1, 1), which notates one on the first die and one on the second die. A total of 36 different outcomes can appear when you roll two dice. Let’s do this step by step.įirst, list out all of the possible outcomes. For example, you could calculate the expected value of rolling a pair of weighted dice. You can use these steps to calculate more sophisticated expected values. The expected value was then just the sum of the values in step 2 multiplied by the probabilities in step 3.