-1

I have multiple dataframes with the same column names. I'd like to merge them, but rename the columns based on the names of the dataframes.

Current State:

Dataframe1:

Date          Price
12/1/1990     10.00
12/2/1990     11.00
12/3/1990     12.00

Dataframe2:

Date          Price
12/1/1990     11.00
12/2/1990     12.00
12/3/1990     14.00

Desired State:

Date          DataFrame1Price   DataFrame2Price
12/1/1990     10.00             11.00
12/2/1990     11.00             12.00
12/3/1990     12.00             14.00
user6883405
  • 393
  • 3
  • 14
  • 2
    You have to divide your task into smaller doable tasks. First, merge dataframe, then rename columns. 1. [How to merge dataframes](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right); 2. [How to rename columns](https://stackoverflow.com/questions/7531868/how-to-rename-a-single-column-in-a-data-frame/7532464). – pogibas Jan 29 '18 at 18:36

2 Answers2

3
(Dataframe1 <- data.frame(Date = c('12/1/1990' , '12/2/1990' , '12/3/1990'), 
                          Price = c(10, 11, 12)))
#>        Date Price
#> 1 12/1/1990    10
#> 2 12/2/1990    11
#> 3 12/3/1990    12
(Dataframe2 <- data.frame(Date = c('12/1/1990' , '12/2/1990' , '12/3/1990'), 
                          Price = c(11, 12, 14)))
#>        Date Price
#> 1 12/1/1990    11
#> 2 12/2/1990    12
#> 3 12/3/1990    14

merge(Dataframe1, Dataframe2, by = 'Date', suffixes = c(".Dataframe1",".Dataframe2"))
#>        Date Price.Dataframe1 Price.Dataframe2
#> 1 12/1/1990               10               11
#> 2 12/2/1990               11               12
#> 3 12/3/1990               12               14

or with _join from dplyr

> # install.packages(c("tidyverse"), dependencies = TRUE)
library(dplyr)

Dataframe2 %>% 
       full_join(Dataframe2, 
               by = c("Date"), suffix = c(".Dataframe2", ".Dataframe2"))
> #        Date Price.Dataframe2 Price.Dataframe2.Dataframe2
> # 1 12/1/1990               11                          11
> # 2 12/2/1990               12                          12
> # 3 12/3/1990               14                          14
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
0

A approach to solving your problem would be to bind the rows in long format with an id representing the source data frame and then using spread to switch to wide format as required.

library(tidyr)
library(dplyr)
library(lubridate)

df1 <- data.frame(
  Date = c('12/1/1990' , '12/2/1990' , '12/3/1990'),
  Price = c(10, 11, 12)
)

df2 <- data.frame(
  Date = c('12/1/1990' , '12/2/1990' , '12/3/1990'),
  Price = c(11, 12, 14)
)

df <- bind_rows(df1 = df1, df2 = df2, .id = "source") %>%
  as_tibble %>%
  transmute(
    source,
    date = mdy(Date),
    price = Price
  )

df

# # A tibble: 6 x 3
#   source date       price
#   <chr>  <date>     <dbl>
# 1 df1    1990-12-01  10.0
# 2 df1    1990-12-02  11.0
# 3 df1    1990-12-03  12.0
# 4 df2    1990-12-01  11.0
# 5 df2    1990-12-02  12.0
# 6 df2    1990-12-03  14.0

df %>% spread(source, price)

# # A tibble: 3 x 3
#   date         df1   df2
#   <date>     <dbl> <dbl>
# 1 1990-12-01  10.0  11.0
# 2 1990-12-02  11.0  12.0
# 3 1990-12-03  12.0  14.0
Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40