- 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
Find the product of vector elements in pairs moving forward in R.
To find the product of vector elements in pairs moving forward, we can use prod function along with combn function.
For Example, if we have a vector called V and we want to find the product of elements of V in pairs moving forward then we can use the following command −
combn(V,2,prod)
Example 1
Following snippet creates a sample data frame −
x1<-1:10 x1
If you execute the above given snippet, it generates the following Output −
[1] 1 2 3 4 5 6 7 8 9 10
Add the following code to the above snippet −
x1<-1:10 combn(x1,2,prod)
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 2 3 4 5 6 7 8 9 10 6 8 10 12 14 16 18 20 12 15 18 21 24 27 30 20 [26] 24 28 32 36 40 30 35 40 45 50 42 48 54 60 56 63 70 72 80 90
Example 2
Following snippet creates a sample data frame −
x2<-1:20 x2
If you execute the above given snippet, 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 20
Add the following code to the above snippet −
x2<-1:20 combn(x2,2,prod)
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [19] 20 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 [37] 40 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 [55] 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 30 35 [73] 40 45 50 55 60 65 70 75 80 85 90 95 100 42 48 54 60 66 [91] 72 78 84 90 96 102 108 114 120 56 63 70 77 84 91 98 105 112 [109] 119 126 133 140 72 80 88 96 104 112 120 128 136 144 152 160 90 99 [127] 108 117 126 135 144 153 162 171 180 110 120 130 140 150 160 170 180 190 [145] 200 132 143 154 165 176 187 198 209 220 156 168 180 192 204 216 228 240 [163] 182 195 208 221 234 247 260 210 224 238 252 266 280 240 255 270 285 300 [181] 272 288 304 320 306 323 340 342 360 380
Example 3
Following snippet creates a sample data frame −
x3<-rpois(20,5) x3
If you execute the above given snippet, it generates the following Output −
[1] 5 4 8 5 5 7 2 5 3 5 3 6 3 3 7 8 9 4 2 8
Add the following code to the above snippet −
x3<-rpois(20,5) combn(x3,2,prod)
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 20 40 25 25 35 10 25 15 25 15 30 15 15 35 40 45 20 10 40 32 20 20 28 8 20 [26] 12 20 12 24 12 12 28 32 36 16 8 32 40 40 56 16 40 24 40 24 48 24 24 56 64 [51] 72 32 16 64 25 35 10 25 15 25 15 30 15 15 35 40 45 20 10 40 35 10 25 15 25 [76] 15 30 15 15 35 40 45 20 10 40 14 35 21 35 21 42 21 21 49 56 63 28 14 56 10 [101] 6 10 6 12 6 6 14 16 18 8 4 16 15 25 15 30 15 15 35 40 45 20 10 40 15 [126] 9 18 9 9 21 24 27 12 6 24 15 30 15 15 35 40 45 20 10 40 18 9 9 21 24 [151] 27 12 6 24 18 18 42 48 54 24 12 48 9 21 24 27 12 6 24 21 24 27 12 6 24 [176] 56 63 28 14 56 72 32 16 64 36 18 72 8 32 16
Example 4
Following snippet creates a sample data frame −
x4<-rpois(20,20) x4
If you execute the above given snippet, it generates the following Output −
[1] 24 23 21 20 23 21 25 16 12 26 13 18 20 26 13 23 19 20 18 25
Add the following code to the above snippet −
x4<-rpois(20,20) combn(x4,2,prod)
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 552 504 480 552 504 600 384 288 624 312 432 480 624 312 552 456 480 432 [19] 600 483 460 529 483 575 368 276 598 299 414 460 598 299 529 437 460 414 [37] 575 420 483 441 525 336 252 546 273 378 420 546 273 483 399 420 378 525 [55] 460 420 500 320 240 520 260 360 400 520 260 460 380 400 360 500 483 575 [73] 368 276 598 299 414 460 598 299 529 437 460 414 575 525 336 252 546 273 [91] 378 420 546 273 483 399 420 378 525 400 300 650 325 450 500 650 325 575 [109] 475 500 450 625 192 416 208 288 320 416 208 368 304 320 288 400 312 156 [127] 216 240 312 156 276 228 240 216 300 338 468 520 676 338 598 494 520 468 [145] 650 234 260 338 169 299 247 260 234 325 360 468 234 414 342 360 324 450 [163] 520 260 460 380 400 360 500 338 598 494 520 468 650 299 247 260 234 325 [181] 437 460 414 575 380 342 475 360 500 450
Example 5
Following snippet creates a sample data frame −
x5<-rpois(20,100) x5
If you execute the above given snippet, it generates the following Output −
[1] 125 90 101 99 115 94 106 93 108 85 102 99 115 107 104 117 92 125 93 [20] 104
Add the following code to the above snippet −
x5<-rpois(20,100) combn(x5,2,prod)
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 11250 12625 12375 14375 11750 13250 11625 13500 10625 12750 12375 14375 [13] 13375 13000 14625 11500 15625 11625 13000 9090 8910 10350 8460 9540 [25] 8370 9720 7650 9180 8910 10350 9630 9360 10530 8280 11250 8370 [37] 9360 9999 11615 9494 10706 9393 10908 8585 10302 9999 11615 10807 [49] 10504 11817 9292 12625 9393 10504 11385 9306 10494 9207 10692 8415 [61] 10098 9801 11385 10593 10296 11583 9108 12375 9207 10296 10810 12190 [73] 10695 12420 9775 11730 11385 13225 12305 11960 13455 10580 14375 10695 [85] 11960 9964 8742 10152 7990 9588 9306 10810 10058 9776 10998 8648 [97] 11750 8742 9776 9858 11448 9010 10812 10494 12190 11342 11024 12402 [109] 9752 13250 9858 11024 10044 7905 9486 9207 10695 9951 9672 10881 [121] 8556 11625 8649 9672 9180 11016 10692 12420 11556 11232 12636 9936 [133] 13500 10044 11232 8670 8415 9775 9095 8840 9945 7820 10625 7905 [145] 8840 10098 11730 10914 10608 11934 9384 12750 9486 10608 11385 10593 [157] 10296 11583 9108 12375 9207 10296 12305 11960 13455 10580 14375 10695 [169] 11960 11128 12519 9844 13375 9951 11128 12168 9568 13000 9672 10816 [181] 10764 14625 10881 12168 11500 8556 9568 11625 13000 9672