rep
Replicate Data Values

Description

Replicates the input either a specified number of times or to a specified length.

Usage

rep(x, ...)
rep.int(x, times)
rep_len(x, length.out)
## Default S3 method:
rep(x=NULL, times = 1, length.out = NA, each = 1)
## S3 method for class 'factor':
rep(x, ...) 
## S3 method for class 'Date':
rep(x, ...) 
## S3 method for class 'POSIXct':
rep(x, ...)
## S3 method for class 'POSIXlt':
rep(x, ...)

Arguments

x a vector. Missing values (NAs) are allowed.
... additional arguments to be passed to other methods.
times the number of times to replicate x. You can specify either a single value or a numeric vector.
  • If you specify a single value, the whole of x is replicated that many times.
  • If you specify a vector the same length as x, the result is a vector with times[1] replications of x[1], times[2] of x[2], and so on.
You can specify zero in both usages. If times = 0, then the length of the result is 0.

An error is returned if the length of times is neither 1 nor the length of x.

length.out the length of the result. You can use this argument instead of times, in which case x is replicated as needed to produce a result with length.out data values.

times is ignored if you specify values for both length.out and times.

each the number of times to repeat each element of x. This vector is repeated if you specify a value for either times or length.out.

Details

rep is generic with methods for factors and dates, for example. Missing values (NAs) and Infs are treated the same as other values.
rep.int and rep_len are specialized versions for replicating x just given the times or length arguments.
Value
returns a vector of the same class as x with the data values in x replicated according to the arguments times or length.out and each. Any names are removed.
SNext S-PLUS diff
[cawang, 2011-06-13]: In TIBCO Enterprise Runtime for R rep.Date is methods of generic function, In S-PLUS, rep is defined unrestrained by class.
See Also
c, unique, attr, Date, POSIXt
Examples
rep(0, 100)    # 100 zeros 
# 1, 2, 3, 4 repeated until there are 48 numbers  
rep(1:4, length.out=48)  
rep(1:10, 10)    # 10 repetitions of 1:10 
rep(1:10, 1:10)  # 1, 2, 2, 3, 3, 3, ... 
rep(1:5, each=2) # 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 
rep(1:3, 5, each=2) # same as rep( c(1, 1, 2, 2, 3, 3), 5) 

## Date/POSIXt rep(as.Date(c("1970-01-01", "2038-01-19")), length.out = 5) rep(as.POSIXct(c("1970-01-01", "2038-01-19")), times = 2, each = 3) rep(as.POSIXlt(c("1970-01-01", "2038-01-19")), length.out = 5, each = 3)

rep.int(1:3, 1:3) # 1, 2, 2, 3, 3, 3 rep_len(1:3, 5) # 1, 2, 3, 1, 2

Package base version 6.0.0-69
Package Index