dplyr::bind_cols()
   get_help() docs


Description

The bind_cols() function is part of the {dplyr} package, which is part of the {tidyverse}.

This function is used to bind the columns of two tibbles (data frames) or arrays into one tibble (data frame). This requires every column to be of equal length.

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::bind_cols() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::bind_cols()

Conceptual Usage

# Remember that both tibbles (or both arrays) must have the same number of rows in the same order to be able to bind them

bind_cols(first tibble, second tibble)

bind_cols(first array, second array)

Examples

The examples below use the msleep dataset. Learn more about this dataset with get_help("msleep").

# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
##   name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake  brainwt  bodywt
##   <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>   <dbl>
## 1 Owl … Aotus omni  Prim… <NA>                17         1.8      NA       7    0.0155    0.48 
## 2 Moun… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6 NA         1.35 
## 3 Grea… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1  0.00029   0.019
## 4 Cow   Bos   herbi Arti… domesticated         4         0.7       0.667  20    0.423   600    
## 5 Thre… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6 NA         3.85 
## 6 Nort… Call… carni Carn… vu                   8.7       1.4       0.383  15.3 NA        20.5


# Use bind_cols() to combine two compatible arrays into a single tibble containing those arrays as its columns.
array1 <- 1:3
array2 <- 4:6
bind_cols(column1 = array1, column2 = array2)
## # A tibble: 3 × 2
##   column1 column2
##     <int>   <int>
## 1       1       4
## 2       2       5
## 3       3       6


# Use bind_cols() to combine two compatible tibbles into a single tibble containing all columns

# Create two compatible tibbles, for demonstration of bind_cols()
msleep %>% 
  select(name, vore) -> tibble1 

msleep %>% 
  select(order, sleep_total) -> tibble2 

bind_cols(tibble1, tibble2)
## # A tibble: 61 × 4
##    name                       vore  order        sleep_total
##    <chr>                      <chr> <chr>              <dbl>
##  1 Owl monkey                 omni  Primates            17  
##  2 Mountain beaver            herbi Rodentia            14.4
##  3 Greater short-tailed shrew omni  Soricomorpha        14.9
##  4 Cow                        herbi Artiodactyla         4  
##  5 Three-toed sloth           herbi Pilosa              14.4
##  6 Northern fur seal          carni Carnivora            8.7
##  7 Dog                        carni Carnivora           10.1
##  8 Goat                       herbi Artiodactyla         5.3
##  9 Guinea pig                 herbi Rodentia             9.4
## 10 Grivet                     omni  Primates            10  
## # … with 51 more rows