0

I have two dataframes from two different years, and I would like to merge them to a single dataframe based on common row values from column code present in both the dataframes.

How can I do this using dplyr in R?

DF1

Code Count_2020
A    1
B    2
C    3
D    4
E    5
F    6

DF2

Code Count_2021
A    4
B    8
C    6
D    8
E    10
F    12
G    2
H    3
I    4
J    5
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34

2 Answers2

4

We can use inner_join

library(dplyr)
inner_join(DF1, DF2)
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Base R:

merge(DF1,DF2)

Outuput:

  Code Count_2021 Count_2020
1    A          4          1
2    B          8          2
3    C          6          3
4    D          8          4
5    E         10          5
6    F         12          6
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Base R brother. I tried to learn pivoting with base R and I guess this is the hardest thing ever check this link: https://rstudio-pubs-static.s3.amazonaws.com/610313_0328bc102fc1468ba34a9e2ca4c295be.html – Anoushiravan R Aug 06 '21 at 18:20