# How to add named vectors of different sizes based on names in R?

If we have named vectors but the names come from same family then they cannot be added to each other direct to get the sum of values based on names. To do this, we need to use tapply function. For example, if we have three vectors defined as x, y, and z then firstly they need to be combined as V<&minua;c(x,y,z) and then we can add them as tapply(V,names(V),sum)

## Example1

Live Demo

x1<−1:10
names(x1)<−LETTERS[1:10]
x1

## Output

A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10

## Example

Live Demo

x2<−1:100
names(x2)<−rep(LETTERS[1:5],20)
x2

## Output

A B C D E A B C D E A B C D E A B C D E
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A B C D E A B C D E A B C D E A B C D E
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
A B C D E A B C D E A B C D E A B C D E
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
A B C D E A B C D E A B C D E A B C D E
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
A B C D E A B C D E A B C D E A B C D E
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

## Example

Live Demo

x3<−51:150
names(x3)<−rep(LETTERS[1:20],5)
x3

## Output

A  B  C  D  E  F  G  H  I  J  K  L  M  N  O P  Q  R  S  T
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
A  B  C  D  E  F  G  H   I  J  K  L  M  N  O  P  Q  R  S  T
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
A  B  C  D  E  F  G  H I  J  K L M  N  O P  Q   R S  T
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
A  B  C D  E F G  H I J K  L  M  N  O  P  Q  R  S  T
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

## Example

X<−c(x1,x2,x3)
tapply(X,names(X),sum)

## Output

A B C D E F G H I J K L M N O P
1426 1452 1478 1504 1530 486 492 498 504 510 505 510 515 520 525 530
Q R S T
535 540 545 550

## Example2

Live Demo

y1<−sample(0:50,120,replace=TRUE)
names(y1)<−sample(c("S1","S2","S3","S4","S5"),120,replace=TRUE)
y1

## Output

S3 S4 S2 S1 S2 S4 S5 S5 S4 S2 S3 S2 S5 S5 S5 S4 S4 S2 S1 S1 S5 S4 S5 S2 S1 S5
31 36 11 40 21 6 1 17 48 37 44 14 20 37 8 31 11 2 26 49 50 43 35 32 6 42
S2 S2 S3 S2 S1 S5 S2 S5 S1 S2 S3 S5 S4 S1 S4 S3 S2 S2 S1 S4 S3 S1 S2 S2 S4 S1
37 25 38 33 46 11 18 3 10 19 48 1 40 4 22 22 4 16 32 26 41 17 32 23 24 37
S3 S2 S2 S2 S5 S2 S2 S4 S4 S4 S4 S1 S1 S5 S3 S3 S5 S5 S3 S4 S2 S5 S4 S1 S5 S1
14 45 39 28 23 19 22 39 3 42 15 16 0 24 8 14 39 45 0 31 8 27 10 33 2 41
S2 S4 S3 S2 S5 S4 S2 S5 S5 S5 S4 S5 S4 S4 S4 S5 S2 S4 S3 S2 S4 S4 S2 S4 S5 S1
35 27 10 46 0 43 28 21 13 39 6 23 41 44 46 26 33 23 30 3 25 1 47 4 2 43
S4 S3 S2 S4 S5 S5 S1 S5 S3 S4 S2 S5 S1 S4 S4 S1
40 24 17 7 34 34 47 42 34 25 16 35 41 15 24 35

## Example

Live Demo

y2<−sample(1:10,120,replace=TRUE)
names(y2)<−sample(c("S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13","S14","S15"),120,replace=TRUE)
y2

## Output

S5 S8 S5 S6 S2 S15 S4 S12 S15 S8 S5 S6 S10 S13 S15 S5 S12 S9 S5 S6
8 1 9 1 4 9 3 2 4 6 9 5 8 10 4 7 1 2 1 8
S1 S11 S8 S14 S7 S11 S15 S10 S13 S8 S5 S13 S3 S11 S1 S10 S2 S4 S11 S8
4 5 8 9 2 5 5 1 5 1 8 10 4 10 3 3 4 8 9 10
S2 S12 S8 S1 S13 S6 S2 S10 S6 S4 S9 S8 S4 S10 S5 S11 S12 S10 S5 S11
9 10 5 4 8 5 7 7 3 9 7 3 7 8 2 1 9 5 7 1
S1 S4 S10 S9 S7 S14 S4 S12 S13 S1 S15 S1 S3 S14 S5 S8 S5 S6 S7 S13
1 3 10 8 10 1 7 4 6 6 10 2 10 5 3 8 1 6 10 5
S11 S2 S1 S13 S12 S1 S10 S11 S2 S9 S14 S2 S9 S3 S13 S12 S8 S8 S2 S8
1 4 2 4 1 7 5 8 5 1 3 9 5 7 5 8 2 5 8 6
S2 S5 S1 S14 S6 S6 S2 S1 S7 S11 S8 S6 S5 S2 S5 S13 S3 S15 S5 S8
1 7 6 3 1 3 8 5 10 3 1 6 10 2 10 6 1 8 8 7

## Example

Live Demo

y3<−sample(1:99,120,replace=TRUE)
names(y3)<−sample(c("S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13","S14","S15","S16","S17","S18","S19","S20"),120,replace=TRUE)
y3

## Output

S12 S14 S3 S1 S6 S20 S19 S17 S4 S7 S20 S2 S18 S16 S12 S20 S13 S14 S12 S5
76 12 98 92 7 82 43 66 77 53 99 78 89 23 92 80 64 28 71 62
S16 S10 S3 S2 S2 S13 S10 S7 S13 S18 S20 S15 S16 S13 S9 S7 S3 S19 S11 S15
91 58 50 79 28 10 64 53 13 20 4 10 93 12 47 28 42 92 38 38
S17 S2 S8 S13 S10 S19 S12 S18 S2 S2 S9 S2 S1 S3 S18 S18 S9 S14 S19 S8
94 69 95 57 68 23 51 14 45 73 23 34 15 96 91 85 67 89 95 85
S20 S10 S2 S14 S11 S16 S8 S7 S8 S7 S10 S15 S4 S13 S16 S13 S7 S15 S8 S15
53 91 33 98 69 88 77 37 69 94 91 87 33 54 57 87 22 87 26 87
S8 S16 S12 S18 S5 S1 S7 S13 S18 S12 S6 S14 S19 S1 S8 S4 S20 S8 S1 S18
63 82 3 29 5 2 13 88 89 13 30 22 22 13 5 16 56 21 12 26
S9 S8 S18 S8 S19 S17 S16 S19 S1 S8 S14 S18 S1 S19 S1 S13 S7 S6 S19 S14
35 41 33 20 66 2 83 39 40 94 17 53 49 8 20 5 52 13 56 38

## Example

Y<−c(y1,y2,y3)
tapply(Y,names(Y),sum)

## Output

S1 S10 S11 S12 S13 S14 S15 S16 S17 S18 S19 S2 S20 S3 S4 S5
806 419 150 341 449 325 349 517 162 529 444 1210 374 666 961 811
S6 S7 S8 S9
88 384 659 195

Updated on: 17-Oct-2020

176 Views

Get certified by completing the course