0

I have two data frames of this form:

A

code  population index
1        400       5
...

B

code  rate index
1      50    5
...

I want to merge B in A by code, but I want only to merge rate. Expected output:

C

code  population  rate  index
1      400         50     5
...

How to do that in R?

Gregor Thomas
  • 119,032
  • 17
  • 152
  • 277

1 Answers1

0

Here is an option using dplyr. Here is some information on how to provide a proper reproducible example.

library(tibble)
library(dplyr)

df_a <- tibble(
  code = 1,
  population = 400,
  index = 5
)

df_b <- tibble(
  code = 1,
  rate = 50,
  index = 5
)

df_a %>% 
  inner_join(df_b, by = "code") %>% 
  select(code, population, rate, index.x) %>% 
  rename(index = index.x)

#> # A tibble: 1 x 4
#>    code population  rate index
#>   <dbl>      <dbl> <dbl> <dbl>
#> 1     1        400    50     5

Created on 2020-11-16 by the reprex package (v0.3.0)

iamericfletcher
  • 2,529
  • 6
  • 17