The R Project SVN R

Rev

Rev 24839 | Rev 39399 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 24839 Rev 25099
Line 1... Line 1...
1
\name{sample}
1
\name{sample}
-
 
2
\alias{sample}
2
\title{Random Samples and Permutations}
3
\title{Random Samples and Permutations}
-
 
4
\description{
-
 
5
  \code{sample} takes a sample of the specified size from the elements
-
 
6
  of \code{x} using either with or without replacement.
-
 
7
}
3
\usage{
8
\usage{
4
sample(x, size, replace = FALSE, prob = NULL)
9
sample(x, size, replace = FALSE, prob = NULL)
5
}
10
}
6
\alias{sample}
-
 
7
\arguments{
11
\arguments{
8
  \item{x}{Either a (numeric, complex, character or logical)
12
  \item{x}{Either a (numeric, complex, character or logical) vector of
9
    vector of more than one element from which to
-
 
10
    choose, or a positive integer.}
13
    more than one element from which to choose, or a positive integer.}
11
  \item{size}{non-negative integer giving the number of items to choose.}
14
  \item{size}{non-negative integer giving the number of items to choose.}
12
  \item{replace}{Should sampling be with replacement?}
15
  \item{replace}{Should sampling be with replacement?}
13
  \item{prob}{A vector of probability weights for obtaining the elements
16
  \item{prob}{A vector of probability weights for obtaining the elements
14
    of the vector being sampled.}
17
    of the vector being sampled.}
15
}
18
}
16
\description{
-
 
17
  \code{sample} takes a sample of the specified size from
-
 
18
  the elements of \code{x} using either with or without replacement.
-
 
19
}
-
 
20
\details{
19
\details{
21
  If \code{x} has length 1, sampling takes place from
20
  If \code{x} has length 1, sampling takes place from
22
  \code{1:x}.  \emph{Note} that this convenience feature may lead to
21
  \code{1:x}.  \emph{Note} that this convenience feature may lead to
23
  undesired behaviour when \code{x} is of varying length
22
  undesired behaviour when \code{x} is of varying length
24
  \code{sample(x)}.  See the \code{resample()} example below.
23
  \code{sample(x)}.  See the \code{resample()} example below.
Line 49... Line 48...
49
sample(x,replace=TRUE)
48
sample(x,replace=TRUE)
50
 
49
 
51
# 100 Bernoulli trials
50
# 100 Bernoulli trials
52
sample(c(0,1), 100, replace = TRUE)
51
sample(c(0,1), 100, replace = TRUE)
53
 
52
 
54
## More careful bootstrapping --  Consider this when
53
## More careful bootstrapping --  Consider this when using sample()
55
## using sample() programmatically (i.e. in your function or simulation)!
54
## programmatically (i.e., in your function or simulation)!
56
 
55
 
57
# sample()'s surprise -- example
56
# sample()'s surprise -- example
58
x <- 1:10
57
x <- 1:10
59
    sample(x[x >  8]) # length 2
58
    sample(x[x >  8]) # length 2
60
    sample(x[x >  9]) # oops -- length 10!
59
    sample(x[x >  9]) # oops -- length 10!