I have large dataframe called df
with some ID's.
I have another dataframe (id_list
) with a set of matching ID's and its associated features for each ID. The ID are not sequentally ordered in both dataframes.
Effectively i would like to look up from the larger dataframe df
to the id_list
and add two columns namely Display
and Type
to the current dataframe df
.
There are numerous confusing examples. What could be the most effective way of doing this. I tried using match()
, %in%
and failed miserably.
Here is a reproducible example.
df <- data.frame(Feats = matrix(rnorm(20), nrow = 20, ncol = 5), ID = sample.int(10, 10))
id_list <- data.frame(ID = sample.int(10,10),
Display = sample(c('clear', 'blur'), 20, replace = TRUE),
Type = sample(c('red', 'green', 'blue', 'indigo', 'yellow'), 20, replace = TRUE))
Feats.1 Feats.2 Feats.3 Feats.4 Feats.5 ID
1 3.14944573 -0.52285062 3.14944573 -0.52285062 3.14944573 2
2 -0.41096007 0.38256691 -0.41096007 0.38256691 -0.41096007 1
3 0.03629351 -0.02514005 0.03629351 -0.02514005 0.03629351 7
4 0.91257290 1.35590761 0.91257290 1.35590761 0.91257290 5
5 -0.26927311 -2.10213773 -0.26927311 -2.10213773 -0.26927311 3
6 3.14944573 -0.52285062 3.14944573 -0.52285062 3.14944573 4
7 -0.41096007 0.38256691 -0.41096007 0.38256691 -0.41096007 10
8 0.03629351 -0.02514005 0.03629351 -0.02514005 0.03629351 6
9 0.91257290 1.35590761 0.91257290 1.35590761 0.91257290 8
10 -0.26927311 -2.10213773 -0.26927311 -2.10213773 -0.26927311 9
ID Display Type
1 6 clear indigo
2 1 blur blue
3 7 clear red
4 4 clear red
5 3 blur red
6 10 clear yellow
7 2 clear blue
8 8 blur green
9 5 clear blue
10 9 clear green
The resulting end df should be of size [20 x 8].