0

So, I have a dataframe with a large list of centers. I would like to add a column where each of those centers take a specific value fixed in a second dataframe.

The first database looks like this :

Centre      Date       BL_MC        BL_MP
   <chr>  <date>        <dbl>       <dbl>
 1 0RI    2019-05-19        0           0
 2 0RI    2019-05-22        0           0
 3 0RI    2019-05-29        0           0
 4 0RI    2019-05-31        0           0
5 0RI    2019-06-03        0           0
 6 0RI    2019-06-05        0           0
 7 0RI    2019-06-06        0           0
 8 0RI    2019-06-13        0           0
 9 0RI    2019-06-14        0           0
10 0RI    2019-06-17        0           0
# ... with 563,836 more rows, and 7 more
#   variables: GP_SAV_Retail <dbl>,
#   GP_SAV_W <dbl>, GP_SAV <dbl>,
#   Total_general.x <dbl>, SAV_GP_BL <dbl>,
#   BL_MP <dbl>, QRL <dbl>

The second one :

Centre         Park
   <chr>       <dbl>
 1 1BR           252
 2 1HM           198
 3 1JC           171
 4 1LM           245
 5 1MM           149
 6 1PR           200
 7 1SX            64
 8 1VM           123
 9 2FR           168
10 2MN            94
# ... with 1,453 more rows

The output I would like to obtain is the following :

Centre    Date      BL_MC     BL_MP     Park 
0RI       17/12/2019  45       5         328

I ve tried to merge both dataframe but R doesn't assign the right values. I honestly don't have a clue how to do it, since I'm pretty green in R. Any ideas?

Thank you in advanced

  • 1
    It does seem like `left_join(first, second)` should work, but can't verify without seeing your actual data. The `head()` you posted above wouldn't actually result in any matches. – DaveArmstrong Sep 18 '20 at 13:01
  • If you could make a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) it would help you and the community greatly – csgroen Sep 18 '20 at 13:36

2 Answers2

0

The match function is the one you are looking for (in base R):

db1$park = dn2$park[match(db1$center, db2$center)]

match(db1$center, db2$center) returns the position at which the centers from db1 can be found in db2 (db1 and db2 being your two databases)

glagla
  • 611
  • 4
  • 9
0

I think it's a merge (base function) :

db <- merge(db1, db2, by='center')
Mâlo
  • 21
  • 2