Column Mapping and Binding
5 stars based on
Take a sequence of vector, matrix or data-frame arguments and combine by c olumns or r ows, respectively. These are generic functions with methods for other R classes.
These can be given as named arguments. Other R objects may be coerced as appropriate, binding columns S4 methods may be used: The functions cbind and rbind are S3 generic, with methods for data frames.
The data frame method will be used if at least one argument is a data frame and binding columns rest are vectors or matrices. There can be other methods; in particular, there is one for binding columns series objects. If some of the arguments are of an S4 class, i. In that case, deparse. Expressions are not allowed. Language objects such as formulae and calls and pairlists will be coerced to lists: Any classes the inputs might have are discarded in particular, factors are replaced by their internal codes.
If there are several matrix arguments, they must all have the same number of columns or rows and this will be binding columns 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 binding columns they are recycled only fractionally. When the arguments consist of a mix of matrices and vectors binding columns 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 rbindvectors of zero length including NULL are ignored unless the result would have zero rows columnsfor S compatibility. Zero-extent matrices do not occur in S3 and are not ignored in R. So input vectors have the same length restriction: For the default method, a matrix combining the For cbind rbind the column row names are taken from the colnames rownames of the arguments if these are matrix-like.
Binding columns from the names of the arguments or where those are not supplied and deparse. For cbind row names are taken from the first binding columns with appropriate names: For rbind column names are taken from the first argument with appropriate names: The cbind data frame method is just a wrapper for data. The rbind data frame method first drops all zero-column and zero-row arguments. If that leaves none, it returns the first argument with columns otherwise a zero-column zero-row data frame.
It then takes the classes of the columns from the first data frame, and matches columns by name rather than by position. 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. Binding columns categories integer vectors with levels are promoted to factors. The method dispatching is not done via UseMethodbut by C-internal dispatching.
Therefore there binding columns no need for, e. If we find an applicable method we make sure that it is identical to any method determined for prior arguments. If it is binding columns, we proceed, otherwise we immediately drop through to the default code. If you binding columns 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 all the arguments binding columns either data frames or vectors, and this will result in the coercion of character vectors to factors.