Generate a list of 10 random numbers between 1 and 100:

sample(1:100, 10)
##  [1] 69 91 93 16 86 83 34  4 35 96

Generate a list of 10 random numbers between 1 and 100 allowing repetitions:

sample(1:100, 10, replace=T)
##  [1]  1 67 41 80 92 11 13 51 65 35

Generate a list of 10 random numbers between 1 and 10 allowing repetitions:

sample(1:10, 10, replace=T)
##  [1]  1  3  2  5  3  9 10  1  3  8

Randomly order the numbers 1 to 10:

sample(1:10, 10, replace=F)
##  [1]  3  5 10  4  9  8  6  2  7  1

Generate 10 random numbers using the numbers 1 and 2:

sample(1:2, 10, replace=T)
##  [1] 2 1 2 1 1 2 1 2 1 2

Load the data set StudentSurvey from the package Lock5Data:

require(Lock5Data)
## Loading required package: Lock5Data
data(StudentSurvey)

Select 10 random observations:

sample(StudentSurvey, 10)
##          Year Gender Smoke   Award HigherSAT Exercise TV Height Weight
## 129 Sophomore      F    No Olympic    Verbal        2  3     64    140
## 224 Sophomore      M    No   Nobel    Verbal       12 10     71    138
## 87     Senior      M    No Academy    Verbal        4  5     72    240
## 11  Sophomore      F    No Olympic      Math       12  6     65    140
## 124 Sophomore      M    No Olympic      Math       21 25     69    171
## 1      Senior      M    No Olympic      Math       10  1     71    180
## 138 FirstYear      F    No Olympic      Math       10  2     63    140
## 26  Sophomore      M    No   Nobel      Math       10  5     71    175
## 3   FirstYear      M    No   Nobel      Math       14  5     72    208
## 40     Junior      F    No   Nobel    Verbal        5  5     64    130
##     Siblings BirthOrder VerbalSAT MathSAT  SAT  GPA Pulse Piercings
## 129        2          3       450     400  850 2.50    65         4
## 224        0          1       730     650 1380 3.56    65         0
## 87         1          1       690     550 1240 3.49    70         0
## 11         1          2       500     670 1170 2.09    63         2
## 124        1          1       480     530 1010 2.70    84         0
## 1          4          4       540     670 1210 3.13    54         0
## 138        3          2       520     540 1060 2.80    68         4
## 26         3          2       700     720 1420 3.70    60         0
## 3          2          1       550     560 1110 2.55   130         0
## 40         0         NA       570     570 1140 2.40    48         4
##     orig.ids
## 129      129
## 224      224
## 87        87
## 11        11
## 124      124
## 1          1
## 138      138
## 26        26
## 3          3
## 40        40

Select 10 random observations and save them into the data set sample10. This command will also generates a column named origin.ids that gives information on where in the original data set the random observations fall.

sample10<-sample(StudentSurvey, 10)

Select 10 random observations and save them into the data set sample10. The option origin.ids=F makes it so that no column named origin.ids is generated, so that the sample has no information on where in the original data set the random observations fall.

sample10<-sample(StudentSurvey, 10, orig.ids=F)

Select 5 random observations for which the class year is Junior:

sample(subset(StudentSurvey, Year=="Junior"), 10, orig.ids=F)
##       Year Gender Smoke   Award HigherSAT Exercise TV Height Weight
## 257 Junior      F    No Olympic    Verbal        3  1     66    140
## 140 Junior      F    No Olympic      Math        2  3     67    125
## 220 Junior      M    No Olympic      Math       10 15     71    180
## 331 Junior      M   Yes Olympic      Math        3  3     67    150
## 308 Junior      F    No Olympic                 18  7     67    150
## 251 Junior      M    No Olympic      Math       12  5     70    222
## 53  Junior      M    No Olympic      Math       15  0     72    175
## 4   Junior      M    No   Nobel      Math        3  1     63    110
## 335 Junior      M   Yes   Nobel    Verbal        3  4     75    175
## 173 Junior      F    No   Nobel      Math        5 10     61    110
##     Siblings BirthOrder VerbalSAT MathSAT  SAT  GPA Pulse Piercings
## 257        1          2       580     530 1110 3.30    74         2
## 140        5          4       470     600 1070 3.10    70         2
## 220        1          2       620     640 1260 3.40    54         0
## 331        2          2       620     640 1260 2.90    66         0
## 308        1          1       560     560 1120 3.40    75         5
## 251        2          1       510     730 1240 3.50    66         5
## 53         2          1       650     670 1320 3.60    68         1
## 4          1          1       490     630 1120 3.10    78         0
## 335        0          1       690     500 1190 3.20    61         0
## 173        2          2       660     700 1360 3.83    80         4

Select 5 samples of size 2 using the command do to repeat the random sample selection:

do(5)*sample(StudentSurvey, 2, orig.ids=F)
##          Year Gender Smoke   Award HigherSAT Exercise TV Height Weight
## 126 Sophomore      M    No Olympic      Math        3 15     71    188
## 193 Sophomore      M    No   Nobel    Verbal        3  5     75    200
## 105 Sophomore      M    No Olympic      Math        5  8     72    165
## 138 FirstYear      F    No Olympic      Math       10  2     63    140
## 24  Sophomore      M    No Olympic      Math        7 10     74    200
## 351 Sophomore      F   Yes Olympic      Math        5  3     66    135
## 297    Senior      M    No   Nobel      Math        6 15     65    140
## 231 Sophomore      F    No Olympic    Verbal       25 12     71    193
## 309 Sophomore      M    No Olympic      Math       15  6     71    170
## 225    Senior      M    No Olympic      Math        5  3     71    265
##     Siblings BirthOrder VerbalSAT MathSAT  SAT  GPA Pulse Piercings .row
## 126        3          1       520     600 1120 2.20    66         0    1
## 193        1          1       610     600 1210 3.38    66         0    2
## 105        2          2       640     710 1350 2.80    57         0    1
## 138        3          2       520     540 1060 2.80    68         4    2
## 24         1          2       550     650 1200 3.00    48         0    1
## 351        3          2       570     580 1150 3.25    78         4    2
## 297        3          3       530     640 1170 3.00    88         0    1
## 231        2          2       630     620 1250 3.25    68         0    2
## 309        3          1       580     650 1230 3.25    60         0    1
## 225        1          1       440     460  900 2.70    82         0    2
##     .index
## 126      1
## 193      1
## 105      2
## 138      2
## 24       3
## 351      3
## 297      4
## 231      4
## 309      5
## 225      5

Select 100 samples of 30 students each, and save the mean SAT score for each sample into a data set called sample30SAT:

sample30SAT<-do(100)*mean(~SAT, data=sample(StudentSurvey, 30, orig.ids=F))

Find the mean of the 100 sample mean SAT scores we just created, and compare it to the real mean SAT score of the entire data set:

mean(~result, data=sample30SAT)
## [1] 1202.105
mean(~SAT, data=StudentSurvey)
## [1] 1203.627

The two means are very close to each other indicating that the sample distribution is unbiased.