| Line 32... |
Line 32... |
| 32 |
If \code{x} has length 1, is numeric (in the sense of
|
32 |
If \code{x} has length 1, is numeric (in the sense of
|
| 33 |
\code{\link{is.numeric}}) and \code{x >= 1}, sampling \emph{via}
|
33 |
\code{\link{is.numeric}}) and \code{x >= 1}, sampling \emph{via}
|
| 34 |
\code{sample} takes place from \code{1:x}. \emph{Note} that this
|
34 |
\code{sample} takes place from \code{1:x}. \emph{Note} that this
|
| 35 |
convenience feature may lead to undesired behaviour when \code{x} is
|
35 |
convenience feature may lead to undesired behaviour when \code{x} is
|
| 36 |
of varying length in calls such as \code{sample(x)}. See the examples.
|
36 |
of varying length in calls such as \code{sample(x)}. See the examples.
|
| 37 |
|
37 |
|
| 38 |
Otherwise \code{x} can be any \R object for which \code{length} and
|
38 |
Otherwise \code{x} can be any \R object for which \code{length} and
|
| 39 |
subsetting by integers make sense: S3 or S4 methods for these
|
39 |
subsetting by integers make sense: S3 or S4 methods for these
|
| 40 |
operations will be dispatched as appropriate.
|
40 |
operations will be dispatched as appropriate.
|
| 41 |
|
41 |
|
| 42 |
For \code{sample} the default for \code{size} is the number of items
|
42 |
For \code{sample} the default for \code{size} is the number of items
|
| 43 |
inferred from the first argument, so that \code{sample(x)} generates a
|
43 |
inferred from the first argument, so that \code{sample(x)} generates a
|
| 44 |
random permutation of the elements of \code{x} (or \code{1:x}).
|
44 |
random permutation of the elements of \code{x} (or \code{1:x}).
|
| 45 |
|
45 |
|
| 46 |
As from \R 2.11.0 it is allowed to ask for \code{size = 0} samples
|
46 |
As from \R 2.11.0 it is allowed to ask for \code{size = 0} samples
|
| 47 |
with \code{n = 0} or a length-zero \code{x}, but otherwise \code{n >
|
47 |
with \code{n = 0} or a length-zero \code{x}, but otherwise \code{n >
|
| 48 |
0} or positive \code{length(x)} is required.
|
48 |
0} or positive \code{length(x)} is required.
|
| 49 |
|
49 |
|
| 50 |
Non-integer positive numerical values of \code{n} or \code{x} will be
|
50 |
Non-integer positive numerical values of \code{n} or \code{x} will be
|
| 51 |
truncated to the next smallest integer, which has to be no larger than
|
51 |
truncated to the next smallest integer, which has to be no larger than
|
| 52 |
\code{\link{.Machine}$integer.max}.
|
52 |
\code{\link{.Machine}$integer.max}.
|
| 53 |
|
53 |
|
| 54 |
The optional \code{prob} argument can be used to give a vector of
|
54 |
The optional \code{prob} argument can be used to give a vector of
|
| 55 |
weights for obtaining the elements of the vector being sampled. They
|
55 |
weights for obtaining the elements of the vector being sampled. They
|
| 56 |
need not sum to one, but they should be non-negative and not all zero.
|
56 |
need not sum to one, but they should be non-negative and not all zero.
|
| 57 |
If \code{replace} is true, Walker's alias method (Ripley, 1987) is
|
57 |
If \code{replace} is true, Walker's alias method (Ripley, 1987) is
|
| 58 |
used when there are more than 250 reasonably probable values: this
|
58 |
used when there are more than 250 reasonably probable values: this
|
| Line 61... |
Line 61... |
| 61 |
|
61 |
|
| 62 |
If \code{replace} is false, these probabilities are applied
|
62 |
If \code{replace} is false, these probabilities are applied
|
| 63 |
sequentially, that is the probability of choosing the next item is
|
63 |
sequentially, that is the probability of choosing the next item is
|
| 64 |
proportional to the weights amongst the remaining items. The number
|
64 |
proportional to the weights amongst the remaining items. The number
|
| 65 |
of nonzero weights must be at least \code{size} in this case.
|
65 |
of nonzero weights must be at least \code{size} in this case.
|
| 66 |
|
66 |
|
| 67 |
\code{sample.int} is a bare interface in which both \code{n} and
|
67 |
\code{sample.int} is a bare interface in which both \code{n} and
|
| 68 |
\code{size} must be supplied as integers.
|
68 |
\code{size} must be supplied as integers.
|
| 69 |
|
69 |
|
| 70 |
As from \R 3.0.0, \code{n} can be larger than the largest integer of
|
70 |
As from \R 3.0.0, \code{n} can be larger than the largest integer of
|
| 71 |
type \code{integer}, up to the largest representable integer in type
|
71 |
type \code{integer}, up to the largest representable integer in type
|
| 72 |
\code{double}. Only uniform sampling is supported. Two
|
72 |
\code{double}. Only uniform sampling is supported. Two
|
| 73 |
random numbers are used to ensure uniform sampling of large integers.
|
73 |
random numbers are used to ensure uniform sampling of large integers.
|
| 74 |
}
|
74 |
}
|