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:
# '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?
It may be easier with bind_rows
library(dplyr)
bind_rows(list(A = df1, B = df2), .id = 'id')