cbind in R: How to Bind Data Frame By Columns

The cbind() and rbind() functions are generic methods for data frames. These data frame functions will be used if at least one argument is a data frame and the other are vectors or matrices. To merge two data frames (datasets) horizontally, use the merge() function in the R language.

cbind in R

The cbind() is a built-in R function that takes a sequence of vector, matrix, or data-frame as arguments and combines them by columns. The cbind() function stands for column-bind.

In the default method, all the vectors/matrices must be atomic vectors or lists. Expressions are not allowed.

Syntax

cbind(a1, a2, ..., deparse.level = 1)

Parameters

The cbind() function takes a1,  a2, which takes vectors, matrices, and/or data frames.

deparse.level: for non-matrix, 0 constructs no labels, 1 or 2 constructs labels from the argument names.

Implementing the cbind() function in R

The cbind() function takes a sequence of vector, matrix, or data frame arguments and combines them by columns or rows.

df <- data.frame(c1 = c(1, 2, 3, 4),
 c2 = c(5, 6, 7, 8),
 c3 = c(9, 10, 11, 12))

df
c4 <- c(18, 19, 20, 21)
cat("After adding a column using cbind()", "\n")
newDf <- cbind(df, c4)
newDf

Output

    c1  c2  c3
1   1   5   9
2   2   6  10
3   3   7  11
4   4   8  12
After adding a column using cbind() function
   c1  c2  c3  c4
1  1   5   9   18
2  2   6  10   19
3  3   7  11   20
4  4   8  12   21

In this example, we created a data frame using three vectors and then add a fourth vector as a column to the original data frame using the cbind() function. In the final data frame, we can see that the c4 column is added.

Combine two data frames by columns using cbind()

To combine two data frames by columns, use the cbind() function. The cbind() data frame function is just a wrapper for data.frame(…, check.names = FALSE). This means it will split matrix columns in data frame arguments and convert character columns to factors unless stringsAsFactors = FALSE is specified.

The data frame method will be used if at least one argument is a data frame and the rest are vectors or matrices. 

Let’s create a second data frame and use the cbind() function to merge the second data frame to the first data frame.

df <- data.frame(c1 = c(1, 2, 3, 4),
 c2 = c(5, 6, 7, 8),
 c3 = c(9, 10, 11, 12))
df

df2 <- data.frame(c4 = c(18, 19, 20, 21),
 c5 = c(29, 46, 47, 37))
df2
cat("After adding another data frame using cbind()", "\n")
newDf <- cbind(df, df2)
newDf

Output

   c1 c2 c3
1  1  5  9
2  2  6  10
3  3  7  11
4  4  8  12
  c4  c5
1 18  29
2 19  46
3 20  47
4 21  37
After adding another data frame using cbind()
   c1 c2 c3 c4 c5
1  1  5  9  18 29
2  2  6 10  19 46
3  3  7 11  20 47
4  4  8 12  21 37

You can see that both data frame’s row count is the same, and that is why it merges perfectly, but if the row counts of both columns are different, it will give us the error.

This is because the deparse.level value defines how the column names are generated. The default value of deparse.level is 1.

df <- data.frame(c1 = c(1, 2, 3, 4),
 c2 = c(5, 6, 7, 8),
 c3 = c(9, 10, 11, 12))

df2 <- data.frame(c4 = c(18, 19, 20, 21, 6),
 c5 = c(29, 46, 47, 37, 10))
cat("After adding another data frame using cbind()", "\n")
newDf <- cbind(df, df2)
newDf

Output

After adding another data frame using cbind()
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 4, 5
Calls: cbind -> cbind -> data.frame
Execution halted

We got this error: arguments imply differing number of rows: 4, 5

 If the number of rows of the two data frames differs, the cbind() function cannot be used.

Using cbind() function with Multiple Columns

The cbind() function can also be applied to multiple columns and data objects. For example, define a data frame using three columns and add the two columns to that data frame using the cbind() function, and see the output.

df <- data.frame(c1 = c(1, 2, 3, 4),
 c2 = c(5, 6, 7, 8),
 c3 = c(9, 10, 11, 12))

c4 <- c(18, 19, 20, 21)
c5 <- c(29, 46, 47, 37)
cat("After adding multiple columns using cbind()", "\n")
newDf <- cbind(df, c4, c5)
newDf

Output

After adding multiple columns using cbind()
   c1 c2 c3 c4 c5
1   1  5  9 18 29
2   2  6 10 19 46
3   3  7 11 20 47
4   4  8 12 21 37

Conclusion

The cbind() function, short for column bind, is a merge function that can combine two data frames with the same number of multiple rows into a single data frame.

That is it for the cbind function in R.

See also

How to add columns to a data frame in R

Paste in R

Combine two data frames in R

Remove duplicates in R

Remove rows in R

Leave a Comment