I have two datasets. The 1st one is:
Column_A |
---|
3 |
4 |
1 |
6 |
and the 2nd one looks like this:
Column_B | Column_C |
---|---|
1 | a |
2 | b |
3 | c |
4 | d |
5 | e |
6 | f |
How can I overwrite Column_A from the 1st data frame with observation in Column_C if the value of Column_A is identical as the value of Column_B?
My result should look like this:
Column_A |
---|
c |
d |
a |
f |
Thanks in advance!
CodePudding user response:
You can use match
to find the index of column B that matches each entry in column A. Just subset column C by this.
df1$Column_A <- df2$Column_C[match(df1$Column_A, df2$Column_B)]
df1
#> Column_A
#> 1 c
#> 2 d
#> 3 a
#> 4 f
Reproducible data
df1
#> Column_A
#> 1 3
#> 2 4
#> 3 1
#> 4 6
df2
#> Column_B Column_C
#> 1 1 a
#> 2 2 b
#> 3 3 c
#> 4 4 d
#> 5 5 e
#> 6 6 f