cbind                  package:base                  R Documentation

_C_o_m_b_i_n_e _R _O_b_j_e_c_t_s _b_y _R_o_w_s _o_r _C_o_l_u_m_n_s

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

     Take a sequence of vector, matrix or data frames arguments and
     combine by _c_olumns or _r_ows, respectively.  These are generic
     functions with methods for other R classes.

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

     cbind(..., deparse.level = 1)
     rbind(..., deparse.level = 1)

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

     ...: vectors or matrices.  These can be given as named arguments.

deparse.level: integer controlling the construction of labels;
          currently, '1' is the only possible value.

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

     The functions 'cbind' and 'rbind' are generic, with methods for
     data frames.  The data frame method will be used if an argument is
     a data frame and the rest are vectors or matrices.  There can be
     other methods; in particular, there is one for time series
     objects.

     In the matrix case, all the vectors/matrices must be atomic or
     lists (e.g. not expressions). 

     Data frames can be 'cbind'-ed with matrices, in which case each
     matrix forms a single column in the result, unlike calling
     'data.frame'.

     The 'rbind' data frame method takes the classes of the columns
     from the first data frame.  Factors have their levels expanded as
     necessary (in the order of the levels of the levelsets of the
     factors encountered) and the result is an ordered factor if and
     only if all the components were ordered factors.  (The last point
     differs from S-PLUS.)

     If there are several matrix arguments, they must all have the same
     number of columns (or rows) and this will be the number of columns
     (or rows) of the result.  If all the arguments are vectors, the
     number of columns (rows) in the result is equal to the length of
     the longest vector.  Values in shorter arguments are recycled to
     achieve this length (with a 'warning' if they are recycled only
     _fractionally_).

     When the arguments consist of a mix of matrices and vectors the
     number of columns (rows) of the result is determined by the number
     of columns (rows) of the matrix arguments.  Any vectors have their
     values recycled or subsetted to achieve this length.

     For 'cbind' ('rbind'), vectors of zero length (including 'NULL')
     are ignored unless the result would have zero rows (columns),  for
     S compatibility. (Zero-extent matrices do not occur in S3 and are
     not ignored in R.)

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

     A matrix or data frame combining the '...' arguments column-wise
     or row-wise.

     For 'cbind' ('rbind') the column (row) names are taken from the
     names of the arguments, or where those are not supplied by
     deparsing the expressions given (if that gives a sensible name).
     The names will depend on whether data frames are included: see the
     examples.

_N_o_t_e:

     The method dispatching is _not_ done via 'UseMethod()', but by
     C-internal dispatching. Therefore, there is no need for, e.g.,
     'rbind.default'.

     The dispatch algorithm is described in the source file
     ('.../src/main/bind.c') as

        1.  For each argument we get the list of possible class
           memberships from the class attribute.

        2.  We inspect each class in turn to see if there is an an
           applicable method.

        3.  If we find an applicable method we make sure that it is
           identical to any method determined for prior arguments. If
           it is identical, we proceed, otherwise we immediately drop
           through to the default code.

     If you want to combine other objects with data frames, it may be
     necessary to coerce them to data frames first.  (Note that this
     algorithm can result in calling the data frame method if the
     arguments are all either data frames or vectors, and this will
     result in the coercion of character vectors to factors.)

_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:

     'c' to combine vectors (and lists) as vectors, 'data.frame' to
     combine vectors and matrices as a data frame.

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

     m <- cbind(1, 1:7) # the '1' (= shorter vector) is recycled
     m
     m <- cbind(m, 8:14)[, c(1, 3, 2)] # insert a column 
     m
     cbind(1:7, diag(3))# vector is subset -> warning

     cbind(0, rbind(1, 1:3))
     cbind(I=0, X=rbind(a=1, b=1:3))  # use some names
     xx <- data.frame(I=rep(0,2))
     cbind(xx, X=rbind(a=1, b=1:3))   # named differently

     cbind(0, matrix(1, nrow=0, ncol=4))#> Warning (making sense)
     dim(cbind(0, matrix(1, nrow=2, ncol=0)))#-> 2 x 1

