Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

One-liner to concatenate two data frames with a distinguishing column?

I often find myself creating two similar data frames that I’d like to rbind together but keep track of which one each row came from with a distinguishing column. My typical motif has been

new_df <- rbind(
  cbind(df1, id="A"),
  cbind(df2, id="B")
)

which collapses nicely into a single line for readability but feels clunky and I’d like to do it more elegantly. I’d prefer to avoid defining the new column for each separately on multiple lines like below:

df1$id <- "A"
df2$id <- "B"
new_df <- rbind(df1, df2)

and while I know that you can make this a one-liner by playing with $<- that tends to make it much less readable than the cbind/rbind motif above. The rows also aren’t guaranteed to be unique so I can’t do the classic mutate/ifelse motif I’ve seen recommended elsewhere:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# 'value' is not necessarily unique in the below line
new_df <- cbind(df1, df2) %>% mutate(id = ifelse(something==value, "A", "B")

The problem is often inspired by a process like adding a facetting variable for ggplot – I’ve made two data frames from different processes but would like to plot them using facets which requires a facetting column.

What’s an R-friendly way to rbind two data frames while simultaneously creating a column that tracks which data frame they came from?

>Solution :

It may be easier with bind_rows

library(dplyr)
bind_rows(list(A = df1, B = df2), .id = 'id')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading