rep replicates the values in
x. It is a generic
function, and the (internal) default method is described here.
rep_len are faster simplified versions for
two common cases. Internally, they are generic, so methods can be
defined for them (see InternalMethods).
rep(x, ...) rep.int(x, times) rep_len(x, length.out)
a vector (of any mode including a
further arguments to be passed to or from other methods. For the internal default method these can include:
The default behaviour is as if the call was
rep(x, times = 1, length.out = NA, each = 1)
. Normally just one of the additional
arguments is specified, but if
each is specified with either
of the other two, its replication is performed first, and then that
times consists of a single integer, the result consists of
the whole input repeated this many times. If
times is a
vector of the same length as
x (after replication by
each), the result consists of
times times and
length.out may be given in place of
in which case
x is repeated as many times as is
necessary to create a vector of this length. If both are given,
length.out takes priority and
times is ignored.
Non-integer values of
times will be truncated towards zero.
times is a computed quantity it is prudent to add a small
fuzz or use
round. And analogously for
x has length zero and
length.out is supplied and is
positive, the values are filled in using the extraction rules, that is
NA of the appropriate class for an atomic vector
0 for raw vectors) and
NULL for a list.
An object of the same type as
rep_len return no attributes (except the
class if returning a factor).
The default method of
rep gives the result names (which will
almost always contain duplicates) if
x had names, but retains
no other attributes.
rep.int is a simple case which was provided as a
separate function partly for S compatibility and partly for speed
(especially when names can be dropped). The performance of
has been improved since, but
rep.int is still at least twice as
x has names.
rep.int long precedes making
rep is a primitive, but (partial) matching of argument
names is performed as for normal functions.
For historical reasons
rep (only) works on
result is always
NULL even when
length.out is positive.
Although it has never been documented, these functions have always worked on expression vectors.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
rep(1:4, 2) rep(1:4, each = 2) # not the same. rep(1:4, c(2,2,2,2)) # same as second. rep(1:4, c(2,1,2,1)) rep(1:4, each = 2, length.out = 4) # first 4 only. rep(1:4, each = 2, length.out = 10) # 8 integers plus two recycled 1's. rep(1:4, each = 2, times = 3) # length 24, 3 complete replications rep(1, 40*(1-.8)) # length 7 on most platforms rep(1, 40*(1-.8)+1e-7) # better ## replicate a list fred <- list(happy = 1:10, name = "squash") rep(fred, 5) # date-time objects x <- .leap.seconds[1:3] rep(x, 2) rep(as.POSIXlt(x), rep(2, 3)) ## named factor x <- factor(LETTERS[1:4]); names(x) <- letters[1:4] x rep(x, 2) rep(x, each = 2) rep.int(x, 2) # no names rep_len(x, 10)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.