How to combine a data frame and a named vector if name matches with a column in R?

R ProgrammingServer Side ProgrammingProgramming

If we have a data frame that contains a character column and a named vector which has the same names as in the character column of the data frame then we can combine this data frame and the vector by using match function be appropriately defining the names and the character column. Check out the below example to understand how it can be done.

Example

Consider the below data frame df1 and the vector v1 −

 Live Demo

df1<-data.frame(ID=LETTERS[1:20],x1=rpois(20,2))
df1

Output

  ID  x1
1  A  0
2  B  3
3  C  2
4  D  0
5  E  0
6  F  1
7  G  3
8  H  3
9  I  2
10 J  5
11 K  2
12 L  2
13 M  0
14 N  2
15 O  3
16 P  1
17 Q  1
18 R  3
19 S  2
20 T  2

Example

vector1<-rnorm(20)
names(vector1)<-LETTERS[1:20]
vector1

Output

       A          B           C           D            E         F
-1.65864624 -0.72535815 -0.03602353 -0.20926615 -1.09707354 -1.09361827
      G         H          I           J           K           L
0.24223290 1.50364961 2.29867732 -0.35459921 -1.78608099 -0.72382906
      M         N           O          P           Q          R
0.12124456 0.76206838 -0.43075989 2.25536662 1.59808504 -0.57818726
        S         T
-0.40555227 0.66445552

Matching column ID and vector1 −

Example

df1$vector1<-vector1[match(df1$ID,names(vector1))]
df1

Output

    ID  x1  vector1
1   A  0   -1.65864624
2   B  3   -0.72535815
3   C  2   -0.03602353
4   D  0   -0.20926615
5   E  0   -1.09707354
6   F  1   -1.09361827
7   G  3    0.24223290
8   H  3    1.50364961
9   I  2    2.29867732
10  J  5   -0.35459921
11  K  2   -1.78608099
12  L  2   -0.72382906
13  M  0    0.12124456
14  N  2    0.76206838
15  O  3   -0.43075989
16  P  1    2.25536662
17  Q  1    1.59808504
18  R  3   -0.57818726
19  S  2   -0.40555227
20  T  2    0.66445552

Example

 Live Demo

df2<-data.frame(S.No=letters[1:20],x1=rpois(20,10))
df2

Output

 S.No x1
1  a   9
2  b   8
3  c   9
4  d   6
5  e  13
6  f   3
7  g  13
8  h   9
9  i   9
10 j   6
11 k  12
12 l   6
13 m   7
14 n  11
15 o   6
16 p  21
17 q   9
18 r  13
19 s   7
20 t   8

Example

vector2<-rnorm(20)
names(vector2)<-letters[1:20]
vector2

Output

     a             b            c             d          e           f
-1.547152763 -1.111071378 -1.358744699 -0.462245640 0.167145269 -0.635510940
     g             h            i            j           k           l
0.020973365 0.258797947 0.381109857 -0.001844913 0.198083754 -0.057121043
     m             n            o            p           q           r
-0.319798254 -1.597529879 -0.403896403 0.577767115 -1.237463132 -0.734332249
    s             t
-0.252412566 0.957530025

Matching column S.No and vector2 −

Example

df2$vector2<-vector2[match(df2$S.No,names(vector2))]
df2

Output

   S.No x1  vector2
1  a    9  -1.547152763
2  b    8  -1.111071378
3  c    9  -1.358744699
4  d    6  -0.462245640
5  e   13   0.167145269
6  f    3  -0.635510940
7  g   13   0.020973365
8  h    9   0.258797947
9  i    9   0.381109857
10 j    6  -0.001844913
11 k   12   0.198083754
12 l    6  -0.057121043
13 m    7  -0.319798254
14 n   11  -1.597529879
15 o    6  -0.403896403
16 p   21   0.577767115
17 q    9  -1.237463132
18 r   13  -0.734332249
19 s    7  -0.252412566
20 t    8   0.957530025
raja
Published on 16-Mar-2021 10:26:57
Advertisements