## R: Combining vectors or data frames of unequal length into one data frame

5 stars based on
77 reviews

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, or S4 methods may be used: The functions cbind and rbind are S3 generic, with methods **combining two data frames of different lengths r** data frames. The data frame method will be used if at least one 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. 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 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 combining two data frames of different lengths r.

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 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.

Otherwise from the names combining two data frames of different lengths r the arguments or where those are not supplied and deparse. For cbind row names are taken from the first argument 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.

Old-style categories integer vectors with levels are promoted to factors. The method dispatching is combining two data frames of different lengths r done via UseMethodbut by C-internal dispatching. Therefore there is 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 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 all the arguments are either data frames or vectors, and this will result in the coercion of character vectors to factors.