按行和列 R 合并 df

merge df by rows and columns R

提问人:Sulz 提问时间:10/24/2022 最后编辑:MaëlSulz 更新时间:10/24/2022 访问量:35

问:

我需要一些帮助来合并两个数据帧。 假设我必须对数据帧进行数据帧:

df1 <- data.frame(Date = as.Date(seq(from =1, to = 10,1), origin = '1842-01-01'),
                   A = seq(1, 10, 1), 
                  B = rep(1, 10))


df2 <- data.frame(Date = as.Date(seq(from =1, to = 20,1), origin = '1842-01-01'),
                  A = c(rep(NA, 10) ,seq(1, 10, 1)),
                  B = rep(1, 20),
                  C = rep(2, 20))

现在我想将 df1 “集成”到 df2 中。更准确地说,df1$A 的值应在同一日期转到 df2$A 的“NA”值。B、C 等也是如此。

我原来的 DF 要大得多。

例如,我的 dputs:

    dput(Augsburg[1:5, 1:5])
structure(list(Augsburg_Date = structure(c(-46749, -46745, -46744, 
-46742, -46741), class = "Date"), `Augsburg_G Bayrische 4% Obligation (G)` = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_), 
    `Augsburg_P Bayrische 4% Obligation (P)` = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), `Augsburg_P Bayrische 3,5% Obligation (P)` = c("102.25", 
    NA, "102.25", "102.25", NA), `Augsburg_G Bayrische 3,5% Obligation (G)` = c("102", 
    NA, "102", NA, NA)), row.names = c(NA, 5L), class = "data.frame")

    dput(Berlin_total[1:10, 1:10])
structure(list(Date = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), 
    `Fond-Geldkurse:` = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
    ), `BK Pr. Bank Antheilsscheine` = c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), `GK Pr. Bank Antheilsscheine` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), `BK Pr. Bank Antheilsscheine 4,5%` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), `GK Pr. Bank Antheilsscheine 4,5%` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), `BK Preußisch freiwillige Anleihen 5%` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), `GK Preußisch freiwillige Anleihen 5%` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), `BK Preußisch freiwillige Anleihen 4,5%` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), `GK Preußisch freiwillige Anleihen 4,5%` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_)), row.names = c(NA, 10L), class = "data.frame")
R 合并

评论


答:

2赞 Maël 10/24/2022 #1

您可以使用:rows_update

library(dplyr)
rows_update(df2, df1)

输出

Matching, by = "Date"
         Date  A B C
1  1842-01-02  1 1 2
2  1842-01-03  2 1 2
3  1842-01-04  3 1 2
4  1842-01-05  4 1 2
5  1842-01-06  5 1 2
6  1842-01-07  6 1 2
7  1842-01-08  7 1 2
8  1842-01-09  8 1 2
9  1842-01-10  9 1 2
10 1842-01-11 10 1 2
11 1842-01-12  1 1 2
12 1842-01-13  2 1 2
13 1842-01-14  3 1 2
14 1842-01-15  4 1 2
15 1842-01-16  5 1 2
16 1842-01-17  6 1 2
17 1842-01-18  7 1 2
18 1842-01-19  8 1 2
19 1842-01-20  9 1 2
20 1842-01-21 10 1 2

评论

0赞 Sulz 10/24/2022
谢谢!这很容易不知道那个功能!
0赞 Sulz 10/24/2022
我确实有一个问题:如果不是所有列都匹配:它会给出错误:y 中的所有列都必须存在于 x 中,并切换不匹配的列。它是否仍在更新其余部分,还是只更新那些不匹配的地方?
0赞 Maël 10/24/2022
否,则应使用 中的列子集。yx
0赞 Maël 10/24/2022
顺便说一句,在您的情况下可能更合适(它只会覆盖 NA)。rows_patch