1

Dataframe_1

    Name  Value
    a      4
    b      5
    c      7
    d      9

Dataframe_2

    Name Mass
    a     131
    c     140
    b     110
    d     90

I want the following result:

   Name  Value   Mass
   a      4      131
   b      5      110
   c      7      140
   d      9      90

I did this originally as the following but now I have dataframe_2 that I want to get the value from

   Dataframe_1$Mass <- gsub("a", 131, Dataframe_1$Name)

Thank you for your help

J_Throat
  • 77
  • 1
  • 10

2 Answers2

2

The merge function is the tool you need :) https://stat.ethz.ch/R-manual/R-devel/library/base/html/merge.html

e.g. if your two data.frames are called dt1 and dt2, you would use dtnew <- merge(dt1, dt2, by = "Name")

There are variables for controlling different types of joins e.g. How to join (merge) data frames (inner, outer, left, right)?

Jonny Phelps
  • 2,687
  • 1
  • 11
  • 20
1

You're just looking to merge them? I would just merge:

merged <- merge(dataframe_1,dataframe_2, by="Name", all=TRUE) #all
merged <- merge(dataframe_1,dataframe_2, by="Name", all.x =TRUE) #dataframe 1 full
Hatt
  • 689
  • 4
  • 9
  • 23