# How to concatenate two or more vectors in R?

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.

## Example1

Live Demo

set.seed(999)
x1<−1:10
x1

## Output

[1] 1 2 3 4 5 6 7 8 9 10

## Example

Live Demo

x2<−10:1
x2

## Output

[1] 10 9 8 7 6 5 4 3 2 1

## Example

c(x1,x2)

## Output

[1] 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1


## Example2

Live Demo

x3<−sample(0:5,100,replace=TRUE)
x3

## Output

[1] 2 3 4 0 5 0 1 1 5 2 4 5 5 2 2 1 0 4 2 1 3 0 4 4 0 1 1 3 0 4 2 1 0 0 2 1 1
[38] 3 3 3 0 1 4 0 1 4 5 0 1 4 5 3 4 3 1 2 0 3 4 2 3 0 3 0 3 2 0 4 1 2 0 2 1 5
[75] 4 0 4 4 3 5 1 5 3 5 2 0 4 1 4 4 4 2 3 5 3 2 0 5 4 0

## Example

Live Demo

x4<−sample(1:5,120,replace=TRUE)
x4

## Output

[1] 4 2 3 3 3 2 3 1 1 4 1 1 1 4 3 2 4 3 1 5 2 1 4 3 2 1 2 1 5 5 3 1 4 4 2 4 3
[38] 2 5 2 4 3 3 4 2 4 3 1 4 2 2 2 1 4 3 1 1 2 2 4 2 4 2 3 4 2 1 5 2 4 1 4 4 2
[75] 2 2 1 5 1 1 2 3 2 3 4 3 1 4 2 5 5 3 2 1 3 1 2 2 3 5 2 3 1 1 2 5 5 5 1 2 2
[112] 1 4 2 3 2 2 4 5 1

## Example

c(x3,x4)

## Output

[1] 2 3 4 0 5 0 1 1 5 2 4 5 5 2 2 1 0 4 2 1 3 0 4 4 0 1 1 3 0 4 2 1 0 0 2 1 1
[38] 3 3 3 0 1 4 0 1 4 5 0 1 4 5 3 4 3 1 2 0 3 4 2 3 0 3 0 3 2 0 4 1 2 0 2 1 5
[75] 4 0 4 4 3 5 1 5 3 5 2 0 4 1 4 4 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 5 1 1 2 3 2 3 4 3 1 4 2 5
[101] 5 3 2 1 3 1 2 2 3 5 2 3 1 1 2 5 5 5 1 2 2 1 4 2 3
[126] 2 2 4 5 1 10 9 8 7 6 5 4 3 2 1

## Example

c(x1,x3,x2)

## Output

[1] 1 2 3 4 5 6 7 8 9 10 2 3 4 0 5 0 1 1 5 2 4 5 5 2 2
[26] 1 0 4 2 1 3 0 4 4 0 1 1 3 0 4 2 1 0 0 2 1 1 3 3 3
[51] 0 1 4 0 1 4 5 0 1 4 5 3 4 3 1 2 0 3 4 2 3 0 3 0 3
[76] 2 0 4 1 2 0 2 1 5 4 0 4 4 3 5 1 5 3 5 2 0 4 1 4 4
[101] 4 2 3 5 3 2 0 5 4 0 10 9 8 7 6 5 4 3 2 1

## Example

c(x3,x2)

## Output

[1] 2 3 4 0 5 0 1 1 5 2 4 5 5 2 2 1 0 4 2 1 3 0 4 4 0
[26] 1 1 3 0 4 2 1 0 0 2 1 1 3 3 3 0 1 4 0 1 4 5 0 1 4
[51] 5 3 4 3 1 2 0 3 4 2 3 0 3 0 3 2 0 4 1 2 0 2 1 5 4
[76] 0 4 4 3 5 1 5 3 5 2 0 4 1 4 4 4 2 3 5 3 2 0 5 4 0
[101] 10 9 8 7 6 5 4 3 2 1

## Example3

Live Demo

x5<−sample(51:99,100,replace=TRUE)
x5

## Output

[1] 97 61 72 75 80 57 78 80 84 65 81 95 58 77 72 77 93 84 87 66 71 61 57 99 76
[26] 86 57 53 95 90 93 60 90 72 79 92 81 99 96 55 71 73 57 86 86 86 72 53 93 56
[51] 76 53 53 64 68 98 55 66 64 66 61 90 85 81 92 84 92 58 74 69 94 74 88 67 99
[76] 76 73 94 97 85 59 75 93 57 73 97 53 60 81 52 97 75 86 93 99 93 68 67 86 81

## Example

Live Demo

x6<−sample(95:100,100,replace=TRUE)
x6

## Output

[1] 100 96 98 97 98 97 98 96 99 96 100 99 96 99 99 96 98 96
[19] 99 96 97 100 100 97 99 100 97 97 95 95 98 98 95 95 95 96
[37] 98 98 97 98 97 99 97 98 100 96 96 98 98 99 98 95 97 95
[55] 100 97 98 100 95 99 99 97 95 98 95 97 100 98 96 95 97 95
[73] 97 99 98 96 95 99 95 100 98 97 95 97 97 100 96 95 97 96
[91] 95 98 97 100 95 98 96 98 95 98

## Example

Live Demo

x7<−sample(81:100,100,replace=TRUE)
x7

## Output

[1]  86 94 91 85 89 95 87 100 95 83 81 89 90 81 95 82 89 89
[19] 91 88 86 82 88 94 88 91 81 94 82 85 82 82 90 85 86 87
[37] 83 83 100 94 81 97 95 93 91 86 91 98 90 89 82 81 81 89
[55] 85 86 99 94 100 99 87 98 85 81 90 89 90 91 96 86 91 86
[73] 94 84 89 84 82 91 99 87 97 95 84 96 98 91 93 88 95 99
[91] 90 84 83 91 89 81 82 91 91 97

## Example

c(x6,x1)

## Output

[1] 100 96 98 97 98 97 98 96 99 96 100 99 96 99 99 96 98 96
[19] 99 96 97 100 100 97 99 100 97 97 95 95 98 98 95 95 95 96
[37] 98 98 97 98 97 99 97 98 100 96 96 98 98 99 98 95 97 95
[55] 100 97 98 100 95 99 99 97 95 98 95 97 100 98 96 95 97 95
[73] 97 99 98 96 95 99 95 100 98 97 95 97 97 100 96 95 97 96
[91] 95 98 97 100 95 98 96 98 95 98 1 2 3 4 5 6 7 8
[109] 9 10

## Example

c(x6,x2)

## Output

[1] 100 96 98 97 98 97 98 96 99 96 100 99 96 99 99 96 98 96
[19] 99 96 97 100 100 97 99 100 97 97 95 95 98 98 95 95 95 96
[37] 98 98 97 98 97 99 97 98 100 96 96 98 98 99 98 95 97 95
[55] 100 97 98 100 95 99 99 97 95 98 95 97 100 98 96 95 97 95
[73] 97 99 98 96 95 99 95 100 98 97 95 97 97 100 96 95 97 96
[91] 95 98 97 100 95 98 96 98 95 98 10 9 8 7 6 5 4 3
[109] 2 1

## Example

c(x7,x1)

## Output

[1] 86 94 91 85 89 95 87 100 95 83 81 89 90 81 95 82 89 89
[19] 91 88 86 82 88 94 88 91 81 94 82 85 82 82 90 85 86 87
[37] 83 83 100 94 81 97 95 93 91 86 91 98 90 89 82 81 81 89
[55] 85 86 99 94 100 99 87 98 85 81 90 89 90 91 96 86 91 86
[73] 94 84 89 84 82 91 99 87 97 95 84 96 98 91 93 88 95 99
[91] 90 84 83 91 89 81 82 91 91 97 1 2 3 4 5 6 7 8
[109] 9 10

## Example

c(x5,x2)

## Output

[1] 97 61 72 75 80 57 78 80 84 65 81 95 58 77 72 77 93 84 87 66 71 61 57 99 76
[26] 86 57 53 95 90 93 60 90 72 79 92 81 99 96 55 71 73 57 86 86 86 72 53 93 56
[51] 76 53 53 64 68 98 55 66 64 66 61 90 85 81 92 84 92 58 74 69 94 74 88 67 99
[76] 76 73 94 97 85 59 75 93 57 73 97 53 60 81 52 97 75 86 93 99 93 68 67 86 81
[101] 10 9 8 7 6 5 4 3 2 1

Updated on: 17-Oct-2020

4K+ Views