Home > other >  merge df by rows and columns R
merge df by rows and columns R

Time:10-24

I need some help by merging two dataframes. Suppose I have to dataframe:

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))

Now I would like to "integrate" df1 into df2. More exactly The values of df1$A shall go to the "NA" values of df2$A at the same date. The same with B, C etc.

My original DF is much bigger.

For example my 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")

And

    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")

CodePudding user response:

You can use rows_update:

library(dplyr)
rows_update(df2, df1)

output

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
  • Related