How to check if two vectors are exactly same in R?


To check if two vectors are exactly same, we can use identical function.

For example, if we have two vectors say x and y then we can find whether both of them are exactly same or not by using the command given below −

identical(x,y)

Check out the below examples to understand the result of identical function for two vectors.

Example 1

To check if two vectors are exactly same, use the command given below −

x1<-rpois(100,5)
x1

If you execute the above given command, it generates the following output −

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

To check if two vectors are exactly same, add the following to the above command −

y1<-rpois(100,5)
y1

If you execute all the above given commands as a single program, it generates the following output −

[1] 6 3 5 5 3 3 7 6 6 3 5 10 5 10 6 3 5 6 2 6 6 6 3 3 5
[26] 3 6 11 5 8 2 6 3 3 5 4 5 5 6 7 8 6 1 5 4 5 6 5 3 3
[51] 7 11 4 3 5 3 1 4 3 6 6 3 4 5 4 7 4 3 4 2 4 3 4 5 1
[76] 4 3 5 3 1 6 3 6 5 8 7 2 2 8 4 7 3 6 4 3 10 5 4 2 4

To check if two vectors are exactly same, add the following to the above command −

x1<-rpois(100,5)
y1<-rpois(100,5)
identical(x1,y1)

Output

If you execute all the above given commands as a single program, it generates the following output −

[1] FALSE

Example 2

To check if two vectors are exactly same, use the command given below −

x2<-sample(0:9,150,replace=TRUE)
x2

If you execute the above given command, it generates the following output −

[1] 7 5 1 1 9 3 7 2 7 8 2 7 3 3 1 9 6 4 7 9 3 1 4 2 1 5 5 8 0 8 9 0 7 3 1 9 5
[38] 6 7 9 6 3 7 0 9 7 7 7 0 5 7 0 1 5 9 0 9 4 5 2 5 4 1 9 4 5 9 6 8 2 9 1 5 4
[75] 8 5 7 1 5 0 2 3 9 2 2 4 9 1 6 3 7 7 3 1 5 3 3 1 3 1 9 5 3 1 6 1 0 8 4 0 4
[112] 1 6 4 5 0 8 0 4 6 0 4 3 4 4 2 1 5 8 4 0 2 7 4 1 0 9 6 5 8 4 1 6 8 0 4 6 7
[149] 1 5

To check if two vectors are exactly same, add the following to the above command −

y2<-sample(0:9,150,replace=TRUE)
y2

If you execute all the above given commands as a single program, it generates the following output −

[1] 8 3 3 4 3 2 1 5 5 4 6 2 5 4 5 7 6 2 8 7 2 2 8 1 7 5 1 9 4 8 2 9 3 1 6 3 5
[38] 5 7 6 3 0 0 5 0 7 7 7 2 2 0 2 2 2 1 5 3 0 8 0 7 5 9 8 5 1 0 9 4 7 8 6 5 7
[75] 8 9 0 5 8 3 6 8 3 1 0 4 3 8 6 7 6 5 1 4 0 3 8 6 8 8 6 4 3 0 6 8 4 3 1 5 1
[112] 7 0 1 7 4 7 1 6 3 5 7 1 6 0 9 9 3 3 1 5 5 6 6 4 7 6 2 4 5 0 9 5 8 9 5 9 4
[149] 7 7

To check if two vectors are exactly same, add the following to the above command −

x2<-sample(0:9,150,replace=TRUE)
y2<-sample(0:9,150,replace=TRUE)
identical(x2,y2)

Output

If you execute all the above given commands as a single program, it generates the following output −

[1] FALSE

Example 3

To check if two vectors are exactly same, use the command given below −

x3<-1:150
x3

If you execute the above given command, it generates the following output −

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
[109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
[127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150

To check if two vectors are exactly same, add the following to the above command −

y3<-1:150
y3

If you execute all the above given commands as a single program, it generates the following output −

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
[109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
[127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150

To check if two vectors are exactly same, add the following to the above command −

x3<-1:150
y3<-1:150
identical(x3,y3)

Output

If you execute all the above given commands as a single program, it generates the following output −

[1] TRUE

Example 4

To check if two vectors are exactly same, use the command given below −

x4<-150:1
x4

If you execute the above given command, it generates the following output −

[1] 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133
[19] 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115
[37] 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97
[55] 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79
[73] 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61
[91] 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43
[109] 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25
[127] 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7
[145] 6 5 4 3 2 1

To check if two vectors are exactly same, add the following to the above command −

y4<-1:150
y4

If you execute all the above given commands as a single program, it generates the following output −

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
[109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
[127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150

To check if two vectors are exactly same, add the following to the above command −

x4<-150:1
y4<-1:150
identical(x4,y4)

Output

If you execute all the above given commands as a single program, it generates the following output −

[1] FALSE

Example 5

To check if two vectors are exactly same, use the command given below −

x5<-round(rnorm(150),0)
x5

If you execute the above given command, it generates the following output −

[1] 0 0 -1 -1 0 -1 -1 0 1 2 0 0 0 1 0 0 1 0 -1 0 1 0 0 0 -1
[26] 0 0 0 1 0 -1 0 0 0 1 0 0 2 1 0 -1 2 1 -1 1 -1 0 0 1 0
[51] 1 0 1 1 1 0 -3 0 0 -1 1 1 0 -1 0 -1 1 1 0 -1 0 -1 0 0 0
[76] 2 0 -1 0 0 0 0 1 2 1 1 0 -1 -1 2 1 -1 1 -1 0 -1 0 0 0 2
[101] 0 0 1 1 0 -1 -2 1 -1 -1 1 0 1 2 1 0 -1 0 1 2 0 0 0 2 0
[126] -2 -1 0 1 2 1 -1 0 1 0 -1 -1 1 -1 1 -1 0 -1 0 -1 -1 1 1 1 0

To check if two vectors are exactly same, add the following to the above command −

y5<-round(rnorm(150),0)
y5

If you execute all the above given commands as a single program, it generates the following output −

[1] -2 0 -2 0 -1 0 -1 -1 -1 -1 0 0 -1 -2 -1 1 -2 -1 -1 0 0 0 -1 1 0
[26] 1 0 1 -1 0 0 -2 -1 1 0 -1 0 1 1 -2 1 0 -1 0 -2 1 1 -1 1 -2
[51] 1 0 2 0 -1 1 -1 0 0 -1 0 -3 -1 1 -1 1 0 -2 0 -1 1 0 -1 -1 -1
[76] 1 0 1 1 0 -1 0 0 -1 1 -1 1 1 0 0 -1 -1 0 0 -1 0 0 -2 -2 0
[101] -1 1 -1 -1 0 0 1 -1 -2 -1 1 0 -3 -1 0 -1 1 0 2 -3 0 1 -1 0 0
[126] 1 1 -1 1 1 0 0 0 0 0 -2 -1 1 0 1 -1 -1 0 -1 0 -1 -1 -1 0 0

To check if two vectors are exactly same, add the following to the above command −

x5<-round(rnorm(150),0)
y5<-round(rnorm(150),0)
identical(x5,y5)

Output

If you execute all the above given commands as a single program, it generates the following output −

[1] FALSE

Updated on: 11-Nov-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements