- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replace one vector elements with another vector elements in R?
If the data is incorrectly recorded then we need to replace that with the correct. One such case could be reading a vector with values that belong to another vector. In this type of situation, we should replace the vectors appropriate. In R, it can be easily done by using <- operator.
Example
x1<-1:10 x1
Output
[1] 1 2 3 4 5 6 7 8 9 10
Example
y1<-c(5,15,20,25,30,35,40,45,50,55) y1
Output
[1] 5 15 20 25 30 35 40 45 50 55
Replacing x1 with y1 values
Example
x1<-y1 x1
Output
[1] 5 15 20 25 30 35 40 45 50 55
Example
x2<-sample(0:5,120,replace=TRUE) x2
Output
[1] 5 0 5 5 0 4 5 0 5 0 5 5 0 3 4 3 3 2 2 5 4 5 0 2 2 0 1 5 1 4 4 3 3 2 1 3 1 [38] 5 0 1 3 4 1 5 3 2 4 3 0 3 0 4 3 4 4 5 3 5 3 5 0 4 5 5 4 0 1 3 1 3 5 5 1 0 [75] 4 1 0 4 5 4 2 3 2 5 5 4 1 3 4 1 5 2 4 4 0 4 2 2 1 0 1 1 2 5 4 2 4 1 5 1 2 [112] 3 3 1 5 3 1 2 4 5
Example
y2<-sample(501:505,120,replace=TRUE) y2
Output
[1] 503 505 505 501 502 504 502 501 501 501 504 503 501 505 502 504 504 501 [19] 502 501 505 504 503 503 502 505 505 501 502 505 502 503 505 505 501 503 [37] 502 501 505 504 505 501 503 501 502 505 503 503 502 505 503 502 502 502 [55] 504 502 503 505 503 505 504 502 505 503 505 503 503 502 503 502 504 502 [73] 501 504 503 503 505 502 505 505 503 503 503 503 502 503 503 505 502 504 [91] 501 502 503 502 503 504 501 502 503 502 503 502 504 501 504 503 505 503 [109] 503 504 503 505 504 505 501 502 502 502 503 503
Example
x2<-y2 x2
Output
[1] 503 505 505 501 502 504 502 501 501 501 504 503 501 505 502 504 504 501 [19] 502 501 505 504 503 503 502 505 505 501 502 505 502 503 505 505 501 503 [37] 502 501 505 504 505 501 503 501 502 505 503 503 502 505 503 502 502 502 [55] 504 502 503 505 503 505 504 502 505 503 505 503 503 502 503 502 504 502 [73] 501 504 503 503 505 502 505 505 503 503 503 503 502 503 503 505 502 504 [91] 501 502 503 502 503 504 501 502 503 502 503 502 504 501 504 503 505 503 [109] 503 504 503 505 504 505 501 502 502 502 503 503
Example
x3<-sample(1:100,120,replace=TRUE) x3
Output
[1] 93 42 38 52 14 37 92 66 26 18 97 63 92 27 21 17 51 99 [19] 75 10 41 4 34 32 55 61 84 7 62 27 48 48 28 91 64 77 [37] 48 80 16 48 78 51 89 79 52 2 14 22 24 70 67 99 24 23 [55] 100 20 59 65 18 73 91 86 69 39 22 95 22 39 25 23 57 80 [73] 80 38 37 75 40 14 52 20 25 22 23 21 61 3 43 29 89 67 [91] 17 54 38 61 60 73 57 45 30 36 99 8 59 86 57 82 61 60 [109] 59 75 36 85 50 79 41 14 24 3 29 8
Example
y3<-sample(6:10,120,replace=TRUE) y3
Output
[1] 7 7 9 8 7 6 9 10 6 10 10 8 7 6 9 10 7 8 8 6 7 7 6 9 7 [26] 6 10 9 7 8 9 10 8 9 7 7 10 8 9 6 8 9 8 9 8 6 7 7 7 7 [51] 8 7 10 6 10 10 10 9 6 7 6 6 8 6 7 10 6 8 8 10 10 7 10 7 7 [76] 7 8 8 6 6 6 8 9 6 10 9 10 10 9 10 9 9 6 8 10 6 8 7 6 6 [101] 7 10 6 6 8 7 10 10 6 8 7 7 10 10 6 9 10 10 6 6
Example
x3<-y3 x3
Output
[1] 7 7 9 8 7 6 9 10 6 10 10 8 7 6 9 10 7 8 8 6 7 7 6 9 7 [26] 6 10 9 7 8 9 10 8 9 7 7 10 8 9 6 8 9 8 9 8 6 7 7 7 7 [51] 8 7 10 6 10 10 10 9 6 7 6 6 8 6 7 10 6 8 8 10 10 7 10 7 7 [76] 7 8 8 6 6 6 8 9 6 10 9 10 10 9 10 9 9 6 8 10 6 8 7 6 6 [101] 7 10 6 6 8 7 10 10 6 8 7 7 10 10 6 9 10 10 6 6
Example
x4<-rnorm(50,1,0.03) x4
Output
[1] 0.9831642 1.0252853 1.0145823 0.9746273 1.0424629 1.0136936 0.9439870 [8] 1.0191114 1.0020480 1.0435602 1.0153047 1.0025025 1.0013413 0.9869820 [15] 0.9909892 0.9616664 0.9846913 0.9770553 0.9828976 0.9808355 0.9985416 [22] 1.0129694 1.0338138 0.9618547 0.9953760 0.9299545 1.0045145 1.0133679 [29] 1.0055642 1.0048645 0.9979495 0.9930195 1.0163143 1.0191684 0.9953278 [36] 1.0298341 0.9872596 1.0042094 0.9468006 1.0028024 0.9955087 1.0077598 [43] 1.0055424 0.9761965 1.0119160 0.9792637 0.9872643 1.0349188 1.0587493 [50] 0.9726261
Example
y4<-rnorm(50,5,1) y4
Output
[1] 6.641022 4.633975 6.136472 4.716942 6.135626 4.445801 5.790062 3.866481 [9] 3.608628 3.432833 3.093560 5.514157 5.425699 4.453981 5.910624 4.735689 [17] 6.739598 6.396134 3.526643 5.298362 3.163440 4.048861 5.544507 3.034935 [25] 5.421878 6.677585 3.677699 5.145666 4.727515 3.536030 4.313064 4.000674 [33] 4.867868 4.816071 3.914698 6.588276 3.961661 5.280268 5.841715 3.888689 [41] 4.319245 3.388089 5.442669 5.616212 5.437626 4.257918 5.466426 4.939256 [49] 6.054435 5.969408
Example
x4<-y4 x4
Output
[1] 6.641022 4.633975 6.136472 4.716942 6.135626 4.445801 5.790062 3.866481 [9] 3.608628 3.432833 3.093560 5.514157 5.425699 4.453981 5.910624 4.735689 [17] 6.739598 6.396134 3.526643 5.298362 3.163440 4.048861 5.544507 3.034935 [25] 5.421878 6.677585 3.677699 5.145666 4.727515 3.536030 4.313064 4.000674 [33] 4.867868 4.816071 3.914698 6.588276 3.961661 5.280268 5.841715 3.888689 [41] 4.319245 3.388089 5.442669 5.616212 5.437626 4.257918 5.466426 4.939256 [49] 6.054435 5.969408
Example
x5<-rpois(100,10) x5
Output
[1] 16 12 10 4 8 4 6 12 11 11 9 5 10 13 9 9 7 9 7 10 3 6 11 15 11 [26] 11 8 8 7 9 16 21 9 11 5 10 5 15 9 12 7 9 15 12 12 9 11 16 9 9 [51] 11 13 9 11 10 3 10 9 10 12 8 13 10 8 16 11 11 11 10 10 8 9 13 8 8 [76] 8 7 10 11 11 8 11 12 5 5 10 12 7 11 7 10 14 10 11 10 6 15 13 4 9
Example
y5<-rpois(100,5) y5
Output
[1] 3 5 4 5 4 2 5 3 3 8 6 7 5 6 6 5 1 3 4 4 6 4 5 4 3 [26] 7 6 5 5 4 3 10 5 2 2 7 4 3 7 1 2 3 4 4 5 4 5 7 7 3 [51] 12 4 6 6 4 7 3 9 6 10 4 3 5 2 9 5 5 5 2 5 4 4 5 3 3 [76] 7 5 9 8 3 5 5 6 0 1 5 6 2 2 6 5 7 7 6 3 3 3 5 1 2
Example
x5<-y5 x5
Output
[1] 3 5 4 5 4 2 5 3 3 8 6 7 5 6 6 5 1 3 4 4 6 4 5 4 3 [26] 7 6 5 5 4 3 10 5 2 2 7 4 3 7 1 2 3 4 4 5 4 5 7 7 3 [51] 12 4 6 6 4 7 3 9 6 10 4 3 5 2 9 5 5 5 2 5 4 4 5 3 3 [76] 7 5 9 8 3 5 5 6 0 1 5 6 2 2 6 5 7 7 6 3 3 3 5 1 2
Advertisements