Binding rows, but not wanting to add columns where there's a mismatch

When using bind_rows I often want one dataset to take precedent, and to ensure those columns are kept and no more added.

On that basis it would be good to have a feature similar to left_join, right_join etc where we have top_join, bottom_join (and only matching columns too maybe?) for binding rows together.

Is there already something that does this?

How about select only the names of your main df from your other dfs and bind the results. You could wrap that up in a function for yourself if you like it.

Exactly what I was thinking

library(tidyverse)

df_1 <- data.frame(x = c(1, 1, 1), y = c("a", "a", "a"))
df_2 <- data.frame(x = c(2, 2, 2), z = c("b", "b", "b"))

top_joined <- df_1 %>%
  bind_rows(df_2) %>%
  select(names(df_1))

bottom_joined <- df_1 %>%
  bind_rows(df_2) %>%
  select(names(df_2))

Thanks for this solution. It's not a problem doing that, it's just an extra step always.

I have just raised it as it feels like an incompleteness to the binding functions in dplyr.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.