# Random Sampling

You use the `sample` function to take a random sample from a vector. This procedure has two basic parameters: the vector to sample from and the number of samples to take.

```sample1 = sample(values, 30)
```

Traditionally, there is one other decision you make when doing random sampling: In a sample without replacmeent , once you've used a value from the vector in the sample, you cannot use it again. Such techniques are appropriate for things like choosing pennies from a jar. In a sample with replacement, you can select the same value multiple times. You might use such a technique if you are sampling from a vector that represents a larger population. By default, R does sampling without replacement. To have it ample with replacment, you add `replace=T`.

````>` `values = seq(from=1,to=10,step=1)`
`>` `values`
`[1]  1  2  3  4  5  6  7  8  9 10`
`>` `sample(values,5)`
`[1] 1 8 9 4 2`
`>` `sample(values,5)`
`[1] 5 2 3 6 8`
`>` `sample(values,5,replace=T)`
`[1]  1 10  8 10  5`
`>` `sample(values,5,replace=T)`
`[1] 10  5  7  4  7`
`>` `sample(values,5,replace=T)`
`[1] 9 4 9 9 6`
`>` `sample(values,5,replace=T)`
`[1]  2  2  2  5 10`
`>` `sample(values,5,replace=T)`
`[1] 9 1 3 6 7`
```

As the example suggests, sampling with replacement can, but need not, duplicate values.

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-8 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.