Home > Enterprise >  merge two data frames with different number of rows and repeat same value for same column
merge two data frames with different number of rows and repeat same value for same column

Time:11-24

I have two data frames named df1 and df2 with different number of rows. in these two data frames, I have three identical columns but with different sizes.

df1<- data_frame(id= c(1,2,2,3,3,3,5,5,4,4), num=c(10,20,20,30,30,30,50,50,40,40), age=c(31,32,32,33,33,33,35,35,34,34),
                 c=c(95,96,96,97,97,97,99,99,98,98))
                   
df2<- data_frame(id=1:5, num=c(10,20,30,40,50), age=31:35,a=11:15,b=10.5:14.5)

I want to merge df1 with df2 based on identical column in such a way that values in column a and b in df2 repeat based on corresponding columns in df1. my expected output would be like this:

     id   num   age     c     a     b
    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1    10    31    95    11  10.5
 2     2    20    32    96    12  11.5
 3     2    20    32    96    12  11.5
 4     3    30    33    97    13  12.5
 5     3    30    33    97    13  12.5
 6     3    30    33    97    13  12.5
 7     5    50    35    99    15  14.5
 8     5    50    35    99    15  14.5
 9     4    40    34    98    14  13.5
10     4    40    34    98    14  13.5

thank you in advance

CodePudding user response:

We could use left_join this way:

library(dplyr)
left_join(df1, df2, by="id") %>% 
  select(-ends_with(".y"), num = num.x, age=age.x) 
      id   num   age     c     a     b
   <dbl> <dbl> <dbl> <dbl> <int> <dbl>
 1     1    10    31    95    11  10.5
 2     2    20    32    96    12  11.5
 3     2    20    32    96    12  11.5
 4     3    30    33    97    13  12.5
 5     3    30    33    97    13  12.5
 6     3    30    33    97    13  12.5
 7     5    50    35    99    15  14.5
 8     5    50    35    99    15  14.5
 9     4    40    34    98    14  13.5
10     4    40    34    98    14  13.5
  •  Tags:  
  • r
  • Related