根据Dataframe名称重命名R中的列

时间:2018-01-29 18:28:28

标签: r dataframe data-binding merge dplyr

我有多个具有相同列名的数据帧。我想合并它们,但是根据数据帧的名称重命名列。

现状:

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

期望状态:

Date          DataFrame1Price   DataFrame2Price
12/1/1990     10.00             11.00
12/2/1990     11.00             12.00
12/3/1990     12.00             14.00

2 个答案:

答案 0 :(得分: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

或来自_join

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

答案 1 :(得分:0)

解决问题的方法是将长格式的行绑定到表示源数据帧的 id ,然后使用spread切换到宽根据需要格式化。

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