'How to replace values in dataframe based on a second dataframe in R?

I have a dataframe df1 with multiple columns, each column representing a species name (sp1, sp2, sp3, ...).

df1

  sp1   sp2   sp3    sp4 
  NA    NA    r1      r1
  NA    NA    1       3
  NA    5     NA      NA
  m4    NA    NA      m2

I would like to replace each value in df1 with values based on a second dataframe, df2. Here, the values in df1 should match df2$scale_nr, and replaced by df2$percentage. Thus, the result should be so that I have my values in df1 based on $percentage in df2.

df2

scale_nr   percentage
r1          1
p1          1
a1          1
m1          1
r2          2
p2          2
a2          2
m2          2
1           10
2           20
3           30
4           40
...

Then after replacement df1 should look like

df1

  sp1   sp2   sp3    sp4
  NA    NA    1       1
  NA    NA    10      30
  NA    50    NA      NA
  4     NA    NA      2

I tried this:

df2$percentage[match(df1$sp1, df2$scale_nr)] # this one works for one column

which works for one column, I know I should be able to do this over all columns easily, but somehow I can't figure it out.

I know I could do it by 'hand', like

df[df == 'Old Value'] <- 'New value' 

but this seems highly inefficient because I have 40 different values that need to be replaced.

Can someone please help me with a solution for this?

r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source