The R language is commonly used for statistical analysis. As such, it contains a robust set of options for randomization. For specific information on sampling from probability distributions, see the documentation for distribution functions.

Users who are coming from other programming languages may be confused by the lack of a `rand`

function equivalent to what they may have experienced before. Basic random number generation is done using the `r*`

family of functions for each distribution (see the link above). Random numbers drawn uniformly from a range can be generated using `runif`

, for "random uniform". Since this also looks suspiciously like "run if", it is often hard to figure out for new R users.

The `sample`

command can be used to simulate classic probability problems like drawing from an urn with and without replacement, or creating random permutations.

Note that throughout this example, `set.seed`

is used to ensure that the example code is reproducible. However, `sample`

will work without explicitly calling `set.seed`

.

In the simplest form, `sample`

creates a random permutation of a vector of integers. This can be accomplished with:

```
set.seed(1251)
sample(x = 10)
[1] 7 1 4 8 6 3 10 5 2 9
```

When given no other arguments, `sample`

returns a random permutation of the vector from 1 to `x`

. This can be useful when trying to randomize the order of the rows in a data frame. This is a common task when creating randomization tables for trials, or when selecting a random subset of rows for analysis.

```
library(datasets)
set.seed(1171)
iris_rand <- iris[sample(x = 1:nrow(iris)),]
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> head(iris_rand)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
145 6.7 3.3 5.7 2.5 virginica
5 5.0 3.6 1.4 0.2 setosa
85 5.4 3.0 4.5 1.5 versicolor
137 6.3 3.4 5.6 2.4 virginica
128 6.1 3.0 4.9 1.8 virginica
105 6.5 3.0 5.8 2.2 virginica
```

Using `sample`

, we can also simulate drawing from a set with and without replacement. To sample without replacement (the default), you must provide sample with a set to be drawn from and the number of draws. The set to be drawn from is given as a vector.

```
set.seed(7043)
sample(x = LETTERS,size = 7)
[1] "S" "P" "J" "F" "Z" "G" "R"
```

Note that if the argument to `size`

is the same as the length of the argument to `x`

, you are creating a random permutation. Also note that you cannot specify a size greater than the length of `x`

when doing sampling without replacement.

```
set.seed(7305)
sample(x = letters,size = 26)
[1] "x" "z" "y" "i" "k" "f" "d" "s" "g" "v" "j" "o" "e" "c" "m" "n" "h" "u" "a" "b" "l" "r" "w" "t" "q" "p"
sample(x = letters,size = 30)
Error in sample.int(length(x), size, replace, prob) :
cannot take a sample larger than the population when 'replace = FALSE'
```

This brings us to drawing with replacement.

To make random draws from a set with replacement, you use the `replace`

argument to `sample`

. By default, `replace`

is `FALSE`

. Setting it to `TRUE`

means that each element of the set being drawn from may appear more than once in the final result.

```
set.seed(5062)
sample(x = c("A","B","C","D"),size = 8,replace = TRUE)
[1] "D" "C" "D" "B" "A" "A" "A" "A"
```

By default, when you use `sample`

, it assumes that the probability of picking each element is the same. Consider it as a basic "urn" problem. The code below is equivalent to drawing a colored marble out of an urn 20 times, writing down the color, and then putting the marble back in the urn. The urn contains one red, one blue, and one green marble, meaning that the probability of drawing each color is 1/3.

```
set.seed(6472)
sample(x = c("Red","Blue","Green"),
size = 20,
replace = TRUE)
```

Suppose that, instead, we wanted to perform the same task, but our urn contains 2 red marbles, 1 blue marble, and 1 green marble. One option would be to change the argument we send to `x`

to add an additional `Red`

. However, a better choice is to use the `prob`

argument to `sample`

.

The `prob`

argument accepts a vector with the probability of drawing each element. In our example above, the probability of drawing a red marble would be 1/2, while the probability of drawing a blue or a green marble would be 1/4.

```
set.seed(28432)
sample(x = c("Red","Blue","Green"),
size = 20,
replace = TRUE,
prob = c(0.50,0.25,0.25))
```

Counter-intuitively, the argument given to `prob`

does not need to sum to 1. R will always transform the given arguments into probabilities that total to 1. For instance, consider our above example of 2 Red, 1 Blue, and 1 Green. You can achieve the same results as our previous code using those numbers:

```
set.seed(28432)
frac_prob_example <- sample(x = c("Red","Blue","Green"),
size = 200,
replace = TRUE,
prob = c(0.50,0.25,0.25))
set.seed(28432)
numeric_prob_example <- sample(x = c("Red","Blue","Green"),
size = 200,
replace = TRUE,
prob = c(2,1,1))
> identical(frac_prob_example,numeric_prob_example)
[1] TRUE
```

The major restriction is that you cannot set all the probabilities to be zero, and none of them can be less than zero.

You can also utilize `prob`

when `replace`

is set to `FALSE`

. In that situation, after each element is drawn, the proportions of the `prob`

values for the remaining elements give the probability for the next draw. In this situation, you must have enough non-zero probabilities to reach the `size`

of the sample you are drawing. For example:

```
set.seed(21741)
sample(x = c("Red","Blue","Green"),
size = 2,
replace = FALSE,
prob = c(0.8,0.19,0.01))
```

In this example, Red is drawn in the first draw (as the first element). There was an 80% chance of Red being drawn, a 19% chance of Blue being drawn, and a 1% chance of Green being drawn.

For the next draw, Red is no longer in the urn. The total of the probabilities among the remaining items is 20% (19% for Blue and 1% for Green). For that draw, there is a 95% chance the item will be Blue (19/20) and a 5% chance it will be Green (1/20).

The `set.seed`

function is used to set the random seed for all randomization functions. If you are using R to create a randomization that you want to be able to reproduce, you should use `set.seed`

first.

```
set.seed(1643)
samp1 <- sample(x = 1:5,size = 200,replace = TRUE)
set.seed(1643)
samp2 <- sample(x = 1:5,size = 200,replace = TRUE)
> identical(x = samp1,y = samp2)
[1] TRUE
```

Note that parallel processing requires special treatment of the random seed, described more elsewhere.

This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0

This website is not affiliated with Stack Overflow

Email: [email protected]