Say I have an original dataset whose values in the first column are from a to d in the alphabet df1
:
a x1
b x2
c x3
d x4
e x5
and then I have another dataset which multiple columns but whose entries reference the columns in the aforementioned dataset df2
---------
A | B | C
---------
a b c
d e
I would like to use a R function to use the unique values in df2
(a,b, c and d above) in order to create a new column in the df1
dataset that references the title of the corresponding column in df2
, i.e. df3
a x1 A
b x2 B
c x3 C
d x4 B
e x5 C
. Working example:
> # data frame with numbers and characters
> df1 = data.frame(unique_values=letters[1:5], other_col=paste(rep("x",5), 1:5, sep=""))
> print(df1)
unique_values other_col
1 a x1
2 b x2
3 c x3
4 d x4
5 e x5
> # Create dataset that is then used to create new column
> df2 = data.frame(A = c("a",NA), B=c("b","d"), C=c("c","e") )
> df2
A B C
1 a b c
2 <NA> d e
# Using df1 and columns referenging the df1 in df2 create df3
library(dplyr)
#df3?