Replace NA with data from another data.frame

I have a df1 with some observations with NA and i need to replace them with data from df2.

For example:

df1

    id var1  var2  var3  var4  var5 
   <int> <chr> <chr> <chr> <chr> <chr>
      1 I     H     A     F     K    
      2 I     C     NA    NA    N    
      3 T     H     NA    NA    T    
      4 V     U     D     B     Y    
      5 X     Y     C     A     P    
      6 R     G     NA    NA    Q    
      7 P     N     NA    NA    G    
      8 P     C     K     B     H    
      9 G     E     B     G     F    
     10 M     Z     L     G     F


df2
    id var3  var4  var6  var7 
  <dbl> <chr> <chr> <chr> <chr>
     2 D     M     Z     D    
     3 E     L     V     B    
     6 I     C     P     V    
     7 N     K     B     B

I need to replace in df1 the values of id 2,3,6 and 7 in the columns var3 and var4 with the value of these observations of the df2.

the expected result would be this :

 id var1  var2  var3  var4  var5 
   <int> <chr> <chr> <chr> <chr> <chr>
      1 I     H     A     F     K    
      2 I     C     D     M    N    
      3 T     H     E     L    T    
      4 V     U     D     B     Y    
      5 X     Y     C     A     P    
      6 R     G     I     C    Q    
      7 P     N     N     K    G    
      8 P     C     K     B     H    
      9 G     E     B     G     F    
     10 M     Z     L     G     F

How can I do it?

You can use tidyr::rows_update()

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.