Bind rows with columns of different names

Hi, I have two data frames with columns of different names which I want to append. It's like I'm looking for the by argument that you get with joins, but can't find an equivalent. Here's what I have right now, is there a more elegant way to do this?

library(tidyverse)

x <- tibble(name = c('Bob', 'Susan', 'Joe'),
            height = c(59, 58, 61))


y <- tibble(NAME = c('George', 'Angela', 'Jon'),
            HEIGHT = c(55, 63, 53))

# This is a no go...
# x %>% bind_rows(y, by = c('name'='NAME', 'height'='HEIGHT'))

# This works but feels clunky
new_colnames <- c('name', 'height')
colnames(x) <- new_colnames
colnames(y) <- new_colnames

x %>% bind_rows(y)
# A tibble: 6 x 2
  name   height
  <chr>   <dbl>
1 Bob        59
2 Susan      58
3 Joe        61
4 George     55
5 Angela     63
6 Jon        53

How about the following?

y %>% 
  rename(name = NAME, height = HEIGHT) %>% 
  bind_rows(x)
#> # A tibble: 6 x 2
#>   name   height
#>   <chr>   <dbl>
#> 1 George     55
#> 2 Angela     63
#> 3 Jon        53
#> 4 Bob        59
#> 5 Susan      58
#> 6 Joe        61
1 Like

Ah, the power of the pipe. Yes that works, thank you!

1 Like

This topic was automatically closed 7 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.