Bind together two data frames by their rows or columns in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Bind together two data frames by their rows or columns in R appeared first on Data Science Tutorials

Bind together two data frames by their rows or columns in R, To join two data frames by their rows, use the bind_rows() function from the dplyr package in R.

Bind together two data frames by their rows or columns in R

Why Python is an Important and Useful Programming Language »

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

Similarly, you may use dplyr’s bind_cols() function to join two data frames based on their columns.

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

The examples that follow demonstrate how to utilize each of these functions in practice.

How to draw heatmap in r: Quick and Easy way – Data Science Tutorials

Example 1: Use bind_rows()

The following code demonstrates how to link three data frames together depending on their rows using the bind_rows() function.

library(dplyr)

Let’s create a data frames

df1 <- data.frame(team=c('A', 'A', 'B', 'B'),
                  points=c(412, 514, 519, 254))
df2 <- data.frame(team=c('A', 'B', 'C', 'C'),
                  points=c(408, 617, 522, 285))
df3 <- data.frame(team=c('A', 'B', 'C', 'C'),
                  assists=c(454, 985, 122, 456))

Now we can row bind together data frames.

Rejection Region in Hypothesis Testing – Data Science Tutorials

bind_rows(df1, df2, df3)
  team points assists
1     A    412      NA
2     A    514      NA
3     B    519      NA
4     B    254      NA
5     A    408      NA
6     B    617      NA
7     C    522      NA
8     C    285      NA
9     A     NA     454
10    B     NA     985
11    C     NA     122
12    C     NA     456

If the data frames do not all have the same column names, this function will automatically fill in missing values with NA.

Example 2: Use bind_cols()

The following code demonstrates how to connect three data frames together depending on their columns using the bind_cols() function.

library(dplyr)

Let’s try to column bind together data frames.

How to perform a one-sample t-test in R? – Data Science Tutorials

bind_cols(df1, df2, df3)
  team...1 points...2 team...3 points...4 team...5 assists
1        A        412        A        408        A     454
2        A        514        B        617        B     985
3        B        519        C        522        C     122
4        B        254        C        285        C     456

The original columns from each data frame appear in the final data frame in the order indicated in the bind_cols() function.

The post Bind together two data frames by their rows or columns in R appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)