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

sample(1:100, 10)
##  [1] 100  85  83  99  66  47   1  51  74  57

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

sample(1:100, 10, replace=T)
##  [1] 69 65 40 49 79 55 48 94  6 30

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

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

Randomly order the numbers 1 to 10:

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

Generate 10 random numbers using the numbers 1 and 2:

sample(1:2, 10, replace=T)
##  [1] 2 1 2 1 1 1 2 2 2 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
## 67  FirstYear      M    No Olympic    Verbal       10 20     72    195
## 273 Sophomore      F    No   Nobel      Math        3  2     NA     NA
## 345 FirstYear      M   Yes   Nobel      Math       11  6     68    135
## 215 Sophomore      M    No Olympic      Math       15 15     68    140
## 144    Junior      M    No Olympic    Verbal       18 20     75    175
## 125 Sophomore      F    No Olympic    Verbal       12  2     80    150
## 94  Sophomore      M    No   Nobel      Math       14  5     73    206
## 287    Senior      M    No Olympic      Math       15  5     73    200
## 42  Sophomore      F    No   Nobel    Verbal       12  4     64    135
## 330 Sophomore      M   Yes Academy      Math        3  5     70    165
##     Siblings BirthOrder VerbalSAT MathSAT  SAT  GPA Pulse Piercings
## 67         2          2       600     600 1200 3.10    60         1
## 273        2          2       610     640 1250 3.50    88         2
## 345        1          1       540     670 1210 3.30    84         0
## 215        1          2       570     580 1150 3.00    64         0
## 144        0          1       560     500 1060 2.93    74         0
## 125        4          1       650     600 1250 3.15    68         2
## 94         1          1       560     660 1220 2.00    48         0
## 287        2          1       570     610 1180 3.00    57         0
## 42         2          3       580     540 1120 2.40    73         7
## 330        3          2       630     680 1310 3.25    77         0
##     orig.ids
## 67        67
## 273      273
## 345      345
## 215      215
## 144      144
## 125      125
## 94        94
## 287      287
## 42        42
## 330      330

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
## 220 Junior      M    No Olympic      Math       10 15     71    180
## 31  Junior      M    No Academy      Math        8  8     62    175
## 192 Junior      F    No   Nobel      Math        8  6     65    140
## 40  Junior      F    No   Nobel    Verbal        5  5     64    130
## 331 Junior      M   Yes Olympic      Math        3  3     67    150
## 321 Junior      M   Yes   Nobel      Math       12  5     75    185
## 53  Junior      M    No Olympic      Math       15  0     72    175
## 14  Junior      F    No   Nobel    Verbal        6  1     68    135
## 325 Junior      F   Yes   Nobel      Math       15 10     64    145
## 46  Junior      M    No Olympic    Verbal        5 14     73    159
##     Siblings BirthOrder VerbalSAT MathSAT  SAT  GPA Pulse Piercings
## 220        1          2       620     640 1260 3.40    54         0
## 31         2          3       610     620 1230 3.50    55         0
## 192        2          3       670     710 1380 3.80    84         2
## 40         0         NA       570     570 1140 2.40    48         4
## 331        2          2       620     640 1260 2.90    66         0
## 321        1          2       670     680 1350 3.00    76         0
## 53         2          1       650     670 1320 3.60    68         1
## 14         2          3       650     650 1300 3.08    66         4
## 325        1          1       480     690 1170 3.50    64         8
## 46         1          1       620     620 1240 3.31    61         0

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
## 71     Senior      F    No Olympic      Math        2  6     67    145
## 127 Sophomore      M    No   Nobel    Verbal       20  6     70    195
## 239 Sophomore      M    No Academy      Math        5 10     74    210
## 170 Sophomore      M    No Olympic      Math        8 10     70    183
## 346 Sophomore      F   Yes   Nobel    Verbal       18  5     69    140
## 29  FirstYear      M    No Olympic      Math       12  6     68    190
## 65  FirstYear      M    No Olympic      Math       15  6     70    175
## 344 Sophomore      M   Yes Olympic      Math        5  5     70    165
## 271 FirstYear      M    No Olympic      Math        3  5     74    145
## 339 Sophomore      M   Yes   Nobel                  6  2     74    185
##     Siblings BirthOrder VerbalSAT MathSAT  SAT  GPA Pulse Piercings .row
## 71         3          2       540     640 1180 3.50    62         5    1
## 127        0          1       640     640 1280 3.50    76         0    2
## 239        1          2       510     530 1040 2.70    78         0    1
## 170        3          2       470     500  970 2.20    84         4    2
## 346        1          1       680     580 1260 3.20    87         4    1
## 29         1          1       550     560 1110 2.50    74         0    2
## 65         2          3       500     540 1040   NA    63         0    1
## 344        1          1       530     600 1130 3.25    70         0    2
## 271        1          2       560     620 1180 3.00    70         0    1
## 339        1          2       600     600 1200 3.48    80         0    2
##     .index
## 71       1
## 127      1
## 239      2
## 170      2
## 346      3
## 29       3
## 65       4
## 344      4
## 271      5
## 339      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] 1203.664
mean(~SAT, data=StudentSurvey)
## [1] 1203.627

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