I have a large dataframe where I have two versions of an id variable (name1
and name2
). I want to somehow link these ids so that in a new (much larger) data frame when I encounter name1
I can create a new column that populates with name2
.
Here is an example. In df1
we have name1
and name2
.
df1 <- data.frame(name1 = c('AAA', 'AAB', 'AAC'), name2 = c('top', 'mid', 'bottom'))
Now I want to make a new larger data frame, df2
where I have only name1
, and create a new column that uses the mapping from df1
to add a new column to df2
, name2
which populates using 'AAA' == 'top'; 'AAB' == 'mid'; 'AAC' == 'bottom'
.
df2 <- data.frame(name1 = c(rep('AAA', 10), rep('AAB', 10), rep('AAC', 10)))
This is obviously a minimal example, the data I am dealing with contains 500k rows and 850 unique ids for name1
and name2
so Im looking for something relatively quick.