How to find all combinations of a vector elements without space in R?


When we want to find the combinations of a vector elements, we can use combn function with size of the combination say 2 for pairs but the result gives space between the values. If we do not want to get the combinations with space then combn function will be used with paste function by collapsing the space as shown in the below examples.

Check out the below examples to understand how it can be done.

Example 1

To find all combinations of a vector elements without space, use the snippet given below −

x1<-rpois(10,5)
x1

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

[1] 9 5 5 6 7 4 2 4 6 6

To find all combinations of a vector elements without space, add the following code to the above snippet −

x1<-rpois(10,5)
combn(x1,2,FUN=paste,collapse='')

Output

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

[1] "95" "95" "96" "97" "94" "92" "94" "96" "96" "55" "56" "57" "54" "52" "54"
[16] "56" "56" "56" "57" "54" "52" "54" "56" "56" "67" "64" "62" "64" "66" "66"
[31] "74" "72" "74" "76" "76" "42" "44" "46" "46" "24" "26" "26" "46" "46" "66"

Example 2

To find all combinations of a vector elements without space, use the snippet given below −

x2<-rpois(15,10)
x2

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

[1] 12 5 11 10 6 8 14 13 14 9 8 13 9 11 7

To find all combinations of a vector elements without space, add the following code to the above snippet −

x2<-rpois(15,10)
combn(x2,2,FUN=paste,collapse='')

Output

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

[1] "125" "1211" "1210" "126" "128" "1214" "1213" "1214" "129" "128"
[11] "1213" "129" "1211" "127" "511" "510" "56" "58" "514" "513"
[21] "514" "59" "58" "513" "59" "511" "57" "1110" "116" "118"
[31] "1114" "1113" "1114" "119" "118" "1113" "119" "1111" "117" "106"
[41] "108" "1014" "1013" "1014" "109" "108" "1013" "109" "1011" "107"
[51] "68" "614" "613" "614" "69" "68" "613" "69" "611" "67"
[61] "814" "813" "814" "89" "88" "813" "89" "811" "87" "1413"
[71] "1414" "149" "148" "1413" "149" "1411" "147" "1314" "139" "138"
[81] "1313" "139" "1311" "137" "149" "148" "1413" "149" "1411" "147"
[91] "98" "913" "99" "911" "97" "813" "89" "811" "87" "139"
[101] "1311" "137" "911" "97" "117"

Example 3

To find all combinations of a vector elements without space, use the snippet given below −

x3<-sample(LETTERS[1:26],20)
x3

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

[1] "Z" "F" "U" "G" "S" "O" "I" "X" "T" "M" "D" "W" "L" "P" "C" "N" "H" "Q" "A"
[20] "B"

To find all combinations of a vector elements without space, add the following code to the above snippet −

x3<-sample(LETTERS[1:26],20)
combn(x3,2,FUN=paste,collapse='')

Output

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

[1] "ZF" "ZU" "ZG" "ZS" "ZO" "ZI" "ZX" "ZT" "ZM" "ZD" "ZW" "ZL" "ZP" "ZC" "ZN"
[16] "ZH" "ZQ" "ZA" "ZB" "FU" "FG" "FS" "FO" "FI" "FX" "FT" "FM" "FD" "FW" "FL"
[31] "FP" "FC" "FN" "FH" "FQ" "FA" "FB" "UG" "US" "UO" "UI" "UX" "UT" "UM" "UD"
[46] "UW" "UL" "UP" "UC" "UN" "UH" "UQ" "UA" "UB" "GS" "GO" "GI" "GX" "GT" "GM"
[61] "GD" "GW" "GL" "GP" "GC" "GN" "GH" "GQ" "GA" "GB" "SO" "SI" "SX" "ST" "SM"
[76] "SD" "SW" "SL" "SP" "SC" "SN" "SH" "SQ" "SA" "SB" "OI" "OX" "OT" "OM" "OD"
[91] "OW" "OL" "OP" "OC" "ON" "OH" "OQ" "OA" "OB" "IX" "IT" "IM" "ID" "IW" "IL"
[106] "IP" "IC" "IN" "IH" "IQ" "IA" "IB" "XT" "XM" "XD" "XW" "XL" "XP" "XC" "XN"
[121] "XH" "XQ" "XA" "XB" "TM" "TD" "TW" "TL" "TP" "TC" "TN" "TH" "TQ" "TA" "TB"
[136] "MD" "MW" "ML" "MP" "MC" "MN" "MH" "MQ" "MA" "MB" "DW" "DL" "DP" "DC" "DN"
[151] "DH" "DQ" "DA" "DB" "WL" "WP" "WC" "WN" "WH" "WQ" "WA" "WB" "LP" "LC" "LN"
[166] "LH" "LQ" "LA" "LB" "PC" "PN" "PH" "PQ" "PA" "PB" "CN" "CH" "CQ" "CA" "CB"
[181] "NH" "NQ" "NA" "NB" "HQ" "HA" "HB" "QA" "QB" "AB"

Example 4

To find all combinations of a vector elements without space, use the snippet given below −

x4<-sample(0:9,20,replace=TRUE)
x4

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

[1] 6 8 8 0 6 8 9 5 3 1 8 8 8 3 2 5 3 9 0 2

To find all combinations of a vector elements without space, add the following code to the above snippet −

x4<-sample(0:9,20,replace=TRUE)
combn(x4,2,FUN=paste,collapse='')

Output

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

[1] "68" "68" "60" "66" "68" "69" "65" "63" "61" "68" "68" "68" "63" "62" "65"
[16] "63" "69" "60" "62" "88" "80" "86" "88" "89" "85" "83" "81" "88" "88" "88"
[31] "83" "82" "85" "83" "89" "80" "82" "80" "86" "88" "89" "85" "83" "81" "88"
[46] "88" "88" "83" "82" "85" "83" "89" "80" "82" "06" "08" "09" "05" "03" "01"
[61] "08" "08" "08" "03" "02" "05" "03" "09" "00" "02" "68" "69" "65" "63" "61"
[76] "68" "68" "68" "63" "62" "65" "63" "69" "60" "62" "89" "85" "83" "81" "88"
[91] "88" "88" "83" "82" "85" "83" "89" "80" "82" "95" "93" "91" "98" "98" "98"
[106] "93" "92" "95" "93" "99" "90" "92" "53" "51" "58" "58" "58" "53" "52" "55"
[121] "53" "59" "50" "52" "31" "38" "38" "38" "33" "32" "35" "33" "39" "30" "32"
[136] "18" "18" "18" "13" "12" "15" "13" "19" "10" "12" "88" "88" "83" "82" "85"
[151] "83" "89" "80" "82" "88" "83" "82" "85" "83" "89" "80" "82" "83" "82" "85"
[166] "83" "89" "80" "82" "32" "35" "33" "39" "30" "32" "25" "23" "29" "20" "22"
[181] "53" "59" "50" "52" "39" "30" "32" "90" "92" "02"

Example 5

To find all combinations of a vector elements without space, use the snippet given below −

x5<-sample(1:100,15)
x5

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

[1] 7 93 95 34 36 47 77 71 57 83 30 24 48 31 68

To find all combinations of a vector elements without space, add the following code to the above snippet −

x5<-sample(1:100,15)
combn(x5,2,FUN=paste,collapse='')

Output

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

[1] "793" "795" "734" "736" "747" "777" "771" "757" "783" "730"
[11] "724" "748" "731" "768" "9395" "9334" "9336" "9347" "9377" "9371"
[21] "9357" "9383" "9330" "9324" "9348" "9331" "9368" "9534" "9536" "9547"
[31] "9577" "9571" "9557" "9583" "9530" "9524" "9548" "9531" "9568" "3436"
[41] "3447" "3477" "3471" "3457" "3483" "3430" "3424" "3448" "3431" "3468"
[51] "3647" "3677" "3671" "3657" "3683" "3630" "3624" "3648" "3631" "3668"
[61] "4777" "4771" "4757" "4783" "4730" "4724" "4748" "4731" "4768" "7771"
[71] "7757" "7783" "7730" "7724" "7748" "7731" "7768" "7157" "7183" "7130"
[81] "7124" "7148" "7131" "7168" "5783" "5730" "5724" "5748" "5731" "5768"
[91] "8330" "8324" "8348" "8331" "8368" "3024" "3048" "3031" "3068" "2448"
[101] "2431" "2468" "4831" "4868" "3168"

Updated on: 09-Nov-2021

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements