6.2 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Combine Two Data Frames in R with Different Columns

How to Combine Two Data Frames in R with Different Columns

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 quickly combine two data frames that have different columns:

library(dplyr)

bind_rows(df1, df2)

The following example shows how to use this function in practice.

Example: Combine Two Data Frames with Different Columns

Suppose we have the following two data frames in R:

#define first data frame
df1 frame(A=c(1, 6, 3, 7, 5),
                  B=c(7, 9, 8, 3, 2),
                  C=c(3, 5, 2, 9, 9))

df1

  A B C
1 1 7 3
2 6 9 5
3 3 8 2
4 7 3 9
5 5 2 9

#define second data frame
df2 frame(B=c(1, 3, 3, 4, 5),
                  C=c(7, 7, 8, 3, 2),
                  D=c(3, 3, 6, 6, 8))

df2

  B C D
1 1 7 3
2 3 7 3
3 3 8 6
4 4 3 6
5 5 2 8

Note that df1 has the following column names:

  • A
  • B
  • C

And note that df2 has the following column names:

  • B
  • C
  • D

The column names don’t match, so the rbind() function in R will throw an error if we attempt to use it.

#attempt to use rbind to row bind data frames
rbind(df1, df2)

Error in match.names(clabs, names(xi)) : 
  names do not match previous names

Instead, we can use the bind_rows() function from the dplyr package to combine these two data frames and simply fill in missing values in the resulting data frame with NA values:

library(dplyr)

#combine df1 and df2
bind_rows(df1, df2)

    A B C  D
1   1 7 3 NA
2   6 9 5 NA
3   3 8 2 NA
4   7 3 9 NA
5   5 2 9 NA
6  NA 1 7  3
7  NA 3 7  3
8  NA 3 8  6
9  NA 4 3  6
10 NA 5 2  8

Additional Resources

How to Combine Two Columns into One in R
How to Stack Data Frame Columns in R

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