15.1 C
London
Friday, July 5, 2024
HomeTidyverse in Rdplyr in RHow to Use bind_rows and bind_cols in dplyr (With Examples)

How to Use bind_rows and bind_cols in dplyr (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the bind_rows() function from the dplyr package in R to bind together two data frames by their rows:

bind_rows(df1, df2, df3, ...)

Similarly, you can use the bind_cols() function from dplyr to bind together two data frames by their columns:

bind_cols(df1, df2, df3, ...)

The following examples show how to use each of these functions in practice.

Example 1: Use bind_rows()

The following code shows how to use the bind_rows() function to bind three data frames together based on their rows:

library(dplyr)

#create data frames
df1 frame(team=c('A', 'A', 'B', 'B'),
                  points=c(12, 14, 19, 24))


df2 frame(team=c('A', 'B', 'C', 'C'),
                  points=c(8, 17, 22, 25))

df3 frame(team=c('A', 'B', 'C', 'C'),
                  assists=c(4, 9, 12, 6))

#row bind together data frames
bind_rows(df1, df2, df3)

   team points assists
1     A     12      NA
2     A     14      NA
3     B     19      NA
4     B     24      NA
5     A      8      NA
6     B     17      NA
7     C     22      NA
8     C     25      NA
9     A     NA       4
10    B     NA       9
11    C     NA      12
12    C     NA       6

Notice that this function automatically fills in missing values with NA if the data frames do not all have the same column names.

Example 2: Use bind_cols()

The following code shows how to use the bind_cols() function to bind three data frames together based on their columns:

library(dplyr)

#create data frames
df1 frame(team=c('A', 'A', 'B', 'B'),
                  points=c(12, 14, 19, 24))


df2 frame(team=c('A', 'B', 'C', 'C'),
                  points=c(8, 17, 22, 25))

df3 frame(team=c('A', 'B', 'C', 'C'),
                  assists=c(4, 9, 12, 6))

#column bind together data frames
bind_cols(df1, df2, df3)

  team points assists steals blocks rebounds
1    A     12       A      8      A        4
2    A     14       B     17      B        9
3    B     19       C     22      C       12
4    B     24       C     25      C        6

Notice that the original columns from each data frame appear in the final data frame in the order that we specified them in the bind_cols() function.

Additional Resources

The following tutorials explain how to bind together data frames using the rbind() and cbind() functions from base R:

The following tutorials explain how to perform other common functions in dplyr:

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories