- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 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
> 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
> 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
> 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
> 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
> 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
> 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"
- Related Articles
- How to create a sequence of values between two vectors in R using corresponding elements?
- How to combine two factor vectors to create one in R?
- How to create a data frame with combinations of values in R?
- How to find different elements between two string vectors in R?
- How to create a table for the number of unique values in list of vectors in R?
- How to print string vectors vertically in R?
- How to match two string vectors if the strings case is different in both the vectors in R?
- How to create a list of regression models for predefined vectors in R?
- How to create combination of multiple vectors in R?
- How to get the combinations for a range of values with repetition in R?
- How to find the number of common words between two string vectors in R?
- How to concatenate two or more vectors in R?
- How to multiply two vectors in R as in mathematics?
- How to combine two vectors while replacing the NA values with the values in the other vector in R?
- How to concatenate string vectors separated with hyphen in R?

Advertisements