如何为 R 中的两个向量中的每个字符串值创建组合?
如果我们有两个字符串向量,每个向量包含两个以上的值,那么为这两个向量中的每个字符串值创建组合就变得有点困难。为此,我们可以使用do.callpaste0 函数expand.grid,如下例所示。
示例
> x1<-LETTERS[1:10]输出结果> y1<-LETTERS[11:26]
> do.call(paste0,expand.grid(x1,y1))
[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"
示例
> 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))
[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"
示例
> x3<-c("G1","G2","G3","G4","G5","G6")输出结果> y3<-c("S1","S2","S3","S4","S5")
> do.call(paste0,expand.grid(x3,y3))
[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"
示例
> x4<-sample(c("India","Russia","UK","China","USA","Egypt","Turkey"),10,replace=TRUE)输出结果> x4
[1] "UK" "USA" "Egypt" "China" "USA" "India" "Turkey" "USA"[9] "Egypt" "UK"
示例
> y4<-sample(c("Pakistan","Ukraine","Australia","Sri Lanka","Nepal","Sudan","Somalia","North Korea","Brunei"),10,replace=TRUE)输出结果> y4
[1] "North Korea" "Ukraine" "Sri Lanka" "North Korea" "North Korea"[6] "North Korea" "Pakistan" "Somalia" "Sudan" "Pakistan"
示例
> do.call(paste0,expand.grid(x4,y4))输出结果
[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"
示例
> x5<-c("Hot","Cold")输出结果> y5<-c("Summer","Spring","Winter","Rainy")
> do.call(paste0,expand.grid(x5,y5))
[1] "HotSummer" "ColdSummer" "HotSpring" "ColdSpring" "HotWinter"[6] "ColdWinter" "HotRainy" "ColdRainy"
以上是 如何为 R 中的两个向量中的每个字符串值创建组合? 的全部内容, 来源链接: utcz.com/z/352716.html