rep                   package:base                   R Documentation

_R_e_p_l_i_c_a_t_e _E_l_e_m_e_n_t_s _o_f _V_e_c_t_o_r_s _a_n_d _L_i_s_t_s

_D_e_s_c_r_i_p_t_i_o_n:

     'rep' replicates the values in 'x'. It is a generic function, and
     the default method is described here.

     'rep.int' is a faster simplified version for the commonest case.

_U_s_a_g_e:

     rep(x, times, ...)

     ## Default S3 method:
     rep(x, times, length.out, each, ...)

     rep.int(x, times)

_A_r_g_u_m_e_n_t_s:

       x: a vector (of any mode including a list) or a pairlist or a
          'POSIXct' or 'POSIXlt' or 'date' object.

   times: optional non-negative integer.  A vector giving the number of
          times to repeat each element if of length 'length(x)', or to
          repeat the whole vector if of length 1.

length.out: optional integer. The desired length of the output vector.

    each: optional integer. Each element of 'x' is repeated 'each'
          times.

     ...: further arguments to be passed to or from other methods.

_D_e_t_a_i_l_s:

     A least one of 'times', 'length.out' and 'each' must be specified,
     and normally exactly one is.  If 'length.out' is given, 'times' is
     ignored. If 'each' is specified with either of the other two, its
     replication is performed first, and then that implied by 'times'
     or 'length.out'.

     If 'times' consists of a single integer, the result consists of
     the values in 'x' repeated this many times. If 'times' is a vector
     of the same length as 'x', the result consists of 'x[1]' repeated
     'times[1]' times, 'x[2]' repeated 'times[2]' times and so on.

     'length.out' may be given in place of 'times', in which case 'x'
     is repeated as many times as is necessary to create a vector of
     this length.

     Non-integer values of 'times' will be truncated towards zero. If
     'times' is a computed quantity it is prudent to add a small fuzz.

     If 'x' has length zero and 'length.out' is supplied and is
     positive, the values are filled in using the extraction rules,
     that is by an 'NA' of the appropriate class for an atomic vector
     ('0' for raw vectors) and 'NULL' for a list.

_V_a_l_u_e:

     A vector of the same class as 'x'.

_N_o_t_e:

     If the original vector has names, these are also replicated and so
     will almost always contain duplicates.  (In contrast, S strips the
     names.)

     Function 'rep.int' is a simple case handled by internal code, and
     provided as a separate function purely for S compatibility.

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_. Wadsworth & Brooks/Cole.

_S_e_e _A_l_s_o:

     'seq', 'sequence'.

_E_x_a_m_p_l_e_s:

     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, len = 4)    # first 4 only.
     rep(1:4, each = 2, len = 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))

