# How to create combinations for each string values in two vectors in R?

If we have two string vectors, each containing more than two values then it becomes a little difficult to create the combinations for each string value in those two vectors. For this purpose, we can make use of do.call function paste0 and expand.grid as shown in the below examples.

## Example

Live Demo

> x1<-LETTERS[1:10]
> y1<-LETTERS[11:26]
> do.call(paste0,expand.grid(x1,y1))

## Output

[1] "AK" "BK" "CK" "DK" "EK" "FK" "GK" "HK" "IK" "JK" "AL" "BL" "CL" "DL" "EL"
[16] "FL" "GL" "HL" "IL" "JL" "AM" "BM" "CM" "DM" "EM" "FM" "GM" "HM" "IM" "JM"
[31] "AN" "BN" "CN" "DN" "EN" "FN" "GN" "HN" "IN" "JN" "AO" "BO" "CO" "DO" "EO"
[46] "FO" "GO" "HO" "IO" "JO" "AP" "BP" "CP" "DP" "EP" "FP" "GP" "HP" "IP" "JP"
[61] "AQ" "BQ" "CQ" "DQ" "EQ" "FQ" "GQ" "HQ" "IQ" "JQ" "AR" "BR" "CR" "DR" "ER"
[76] "FR" "GR" "HR" "IR" "JR" "AS" "BS" "CS" "DS" "ES" "FS" "GS" "HS" "IS" "JS"
[91] "AT" "BT" "CT" "DT" "ET" "FT" "GT" "HT" "IT" "JT" "AU" "BU" "CU" "DU" "EU"
[106] "FU" "GU" "HU" "IU" "JU" "AV" "BV" "CV" "DV" "EV" "FV" "GV" "HV" "IV" "JV"
[121] "AW" "BW" "CW" "DW" "EW" "FW" "GW" "HW" "IW" "JW" "AX" "BX" "CX" "DX" "EX"
[136] "FX" "GX" "HX" "IX" "JX" "AY" "BY" "CY" "DY" "EY" "FY" "GY" "HY" "IY" "JY"
[151] "AZ" "BZ" "CZ" "DZ" "EZ" "FZ" "GZ" "HZ" "IZ" "JZ"

## Example

Live Demo

> x2<-c("India","Russia","UK","China","USA","Egypt","Turkey")
> y2<-c("Pakistan","Ukraine","Australia","Sri Lanka","Nepal","Sudan","Somalia","North Korea","Brunei")
> do.call(paste0,expand.grid(x2,y2))

## Output

[1] "IndiaPakistan" "RussiaPakistan" "UKPakistan"
[4] "ChinaPakistan" "USAPakistan" "EgyptPakistan"
[7] "TurkeyPakistan" "IndiaUkraine" "RussiaUkraine"
[10] "UKUkraine" "ChinaUkraine" "USAUkraine"
[13] "EgyptUkraine" "TurkeyUkraine" "IndiaAustralia"
[16] "RussiaAustralia" "UKAustralia" "ChinaAustralia"
[19] "USAAustralia" "EgyptAustralia" "TurkeyAustralia"
[22] "IndiaSri Lanka" "RussiaSri Lanka" "UKSri Lanka"
[25] "ChinaSri Lanka" "USASri Lanka" "EgyptSri Lanka"
[28] "TurkeySri Lanka" "IndiaNepal" "RussiaNepal"
[31] "UKNepal" "ChinaNepal" "USANepal"
[34] "EgyptNepal" "TurkeyNepal" "IndiaSudan"
[37] "RussiaSudan" "UKSudan" "ChinaSudan"
[40] "USASudan" "EgyptSudan" "TurkeySudan"
[43] "IndiaSomalia" "RussiaSomalia" "UKSomalia"
[46] "ChinaSomalia" "USASomalia" "EgyptSomalia"
[49] "TurkeySomalia" "IndiaNorth Korea" "RussiaNorth Korea"
[52] "UKNorth Korea" "ChinaNorth Korea" "USANorth Korea"
[55] "EgyptNorth Korea" "TurkeyNorth Korea" "IndiaBrunei"
[58] "RussiaBrunei" "UKBrunei" "ChinaBrunei"
[61] "USABrunei" "EgyptBrunei" "TurkeyBrunei"

## Example

Live Demo

> x3<-c("G1","G2","G3","G4","G5","G6")
> y3<-c("S1","S2","S3","S4","S5")
> do.call(paste0,expand.grid(x3,y3))

## Output

[1] "G1S1" "G2S1" "G3S1" "G4S1" "G5S1" "G6S1" "G1S2" "G2S2" "G3S2" "G4S2"
[11] "G5S2" "G6S2" "G1S3" "G2S3" "G3S3" "G4S3" "G5S3" "G6S3" "G1S4" "G2S4"
[21] "G3S4" "G4S4" "G5S4" "G6S4" "G1S5" "G2S5" "G3S5" "G4S5" "G5S5" "G6S5"

## Example

Live Demo

> x4<-sample(c("India","Russia","UK","China","USA","Egypt","Turkey"),10,replace=TRUE)
> x4

## Output

[1] "UK" "USA" "Egypt" "China" "USA" "India" "Turkey" "USA"
[9] "Egypt" "UK"

## Example

Live Demo

> y4<-sample(c("Pakistan","Ukraine","Australia","Sri Lanka","Nepal","Sudan","Somalia","North Korea","Brunei"),10,replace=TRUE)
> y4

## Output

[1] "North Korea" "Ukraine" "Sri Lanka" "North Korea" "North Korea"
[6] "North Korea" "Pakistan" "Somalia" "Sudan" "Pakistan"

## Example

> do.call(paste0,expand.grid(x4,y4))

## Output

[1] "UKNorth Korea" "USANorth Korea" "EgyptNorth Korea"
[4] "ChinaNorth Korea" "USANorth Korea" "IndiaNorth Korea"
[7] "TurkeyNorth Korea" "USANorth Korea" "EgyptNorth Korea"
[10] "UKNorth Korea" "UKUkraine" "USAUkraine"
[13] "EgyptUkraine" "ChinaUkraine" "USAUkraine"
[16] "IndiaUkraine" "TurkeyUkraine" "USAUkraine"
[19] "EgyptUkraine" "UKUkraine" "UKSri Lanka"
[22] "USASri Lanka" "EgyptSri Lanka" "ChinaSri Lanka"
[25] "USASri Lanka" "IndiaSri Lanka" "TurkeySri Lanka"
[28] "USASri Lanka" "EgyptSri Lanka" "UKSri Lanka"
[31] "UKNorth Korea" "USANorth Korea" "EgyptNorth Korea"
[34] "ChinaNorth Korea" "USANorth Korea" "IndiaNorth Korea"
[37] "TurkeyNorth Korea" "USANorth Korea" "EgyptNorth Korea"
[40] "UKNorth Korea" "UKNorth Korea" "USANorth Korea"
[43] "EgyptNorth Korea" "ChinaNorth Korea" "USANorth Korea"
[46] "IndiaNorth Korea" "TurkeyNorth Korea" "USANorth Korea"
[49] "EgyptNorth Korea" "UKNorth Korea" "UKNorth Korea"
[52] "USANorth Korea" "EgyptNorth Korea" "ChinaNorth Korea"
[55] "USANorth Korea" "IndiaNorth Korea" "TurkeyNorth Korea"
[58] "USANorth Korea" "EgyptNorth Korea" "UKNorth Korea"
[61] "UKPakistan" "USAPakistan" "EgyptPakistan"
[64] "ChinaPakistan" "USAPakistan" "IndiaPakistan"
[67] "TurkeyPakistan" "USAPakistan" "EgyptPakistan"
[70] "UKPakistan" "UKSomalia" "USASomalia"
[73] "EgyptSomalia" "ChinaSomalia" "USASomalia"
[76] "IndiaSomalia" "TurkeySomalia" "USASomalia"
[79] "EgyptSomalia" "UKSomalia" "UKSudan"
[82] "USASudan" "EgyptSudan" "ChinaSudan"
[85] "USASudan" "IndiaSudan" "TurkeySudan"
[88] "USASudan" "EgyptSudan" "UKSudan"
[91] "UKPakistan" "USAPakistan" "EgyptPakistan"
[94] "ChinaPakistan" "USAPakistan" "IndiaPakistan"
[97] "TurkeyPakistan" "USAPakistan" "EgyptPakistan"
[100] "UKPakistan"

## Example

Live Demo

> x5<-c("Hot","Cold")
> y5<-c("Summer","Spring","Winter","Rainy")
> do.call(paste0,expand.grid(x5,y5))

## Output

[1] "HotSummer" "ColdSummer" "HotSpring" "ColdSpring" "HotWinter"
[6] "ColdWinter" "HotRainy" "ColdRainy"