Simple Random Sampling

If you want to generate a list of random numbers, use the following steps.

Step 1: Set a seed using set.seed(number). Pick any random number. Technically, this step is optional. However, in a classroom setting, we often set a seed so that all the students obtain the same result. Step 2: Use the following syntax:

sample(minimum number : maximum number, size=how many numbers in sample, replace=TRUE or FALSE)

Sampling Without Replacement

To get a sample without replacement, set replace=FALSE.

set.seed(14)
sample(1:30, size=5, replace=FALSE)
## [1]  9 30 11 20 28

For the scenario in Example 2, the clients corresponding to these numbers are: Farrell’s Antiques, Worldwide Wireless, Fox Studios, Precise Plumbing, and Venetian Gardens Restaurant.

Sampling With Replacement

To get a sample with replacement, set replace=TRUE.

set.seed(14)
sample(1:30, size=5, replace=TRUE)
## [1]  9  9 11 20 11

For the scenario in Example 2, the clients corresponding to these numbers are: Farrell’s Antiques, Farrell’s Antiques, Fox Studios, Precise Plumbing, Fox Studios

Now, rather than generating a list of random numbers and determining the individuals corresponding to those numbers, we could instead take a simple random sample from the data frame that contains a list of the individuals we are sampling from. Remember, we are using the MOSAIC package throughout the course. If you have not done so yet, please install the package using the following command:

install.packages("mosaic")

Once the package is installed, you will need to load it into your session. This must be done for each R session. Think of this as opening software on your computer. The following command will load Mosaic.

library(mosaic)

Now, let’s read Table 3 from Section 1.3 into R.

Table3 <- read.csv("https://sullystats.github.io/Statistics6e/Data/Chapter1/Table3.csv")

The syntax for the command is the same as shown above. However, rather than sampling a range of numbers, we sample from the data frame. The data found in Table 3 in Section 1.3 is loaded in a data frame titled Table3. The following shows how to obtain a random sample without replacement using a seed of 14.

set.seed(14)
sample(Table3,5,replace=FALSE)
##                        Company orig.id
## 9           Farrell's Antiques       9
## 30          Worldwide Wireless      30
## 11                 Fox Studios      11
## 20            Precise Plumbing      20
## 28 Venetian Gardens Restaurant      28

The results are the same as those obtained when sampling without replacement found earlier. Notice the number in the far left column represents the row of the observation.