如何在R中连接以连字符分隔的字符串向量?

字符串向量的串联将在向量中创建值的组合,因此,我们可以将它们用于向量之间的交互。在R中,我们可以使用expand.grid和apply来创建这种类型的组合,如以下示例所示。

例子1

x1<-c("India","Russia","China")

y1<-c("UK","USA","Canada")

apply(expand.grid(x1,y1),1,paste,collapse="-")

输出结果

[1"India-UK" "Russia-UK" "China-UK" "India-USA"

[5"Russia-USA" "China-USA" "India-Canada" "Russia-Canada"

[9"China-Canada"

例子2

x2<-c("Hot","Cold")

y2<-c("Summer","Winter","Spring")

apply(expand.grid(x2,y2),1,paste,collapse="-")

输出结果

[1"Hot-Summer" "Cold-Summer" "Hot-Winter" "Cold-Winter" "Hot-Spring"

[6"Cold-Spring"

例子3

x3<-c("G1","G2","G3","G4","G5")

y3<-c("S1","S2","S3","S4")

z3<-c("P1","P2","P3","P4","P5")

apply(expand.grid(x3,y3,z3),1,paste,collapse="-")

输出结果

[1]  "G1-S1-P1" "G2-S1-P1" "G3-S1-P1" "G4-S1-P1" "G5-S1-P1" "G1-S2-P1"

[7]  "G2-S2-P1" "G3-S2-P1" "G4-S2-P1" "G5-S2-P1" "G1-S3-P1" "G2-S3-P1"

[13"G3-S3-P1" "G4-S3-P1" "G5-S3-P1" "G1-S4-P1" "G2-S4-P1" "G3-S4-P1"

[19"G4-S4-P1" "G5-S4-P1" "G1-S1-P2" "G2-S1-P2" "G3-S1-P2" "G4-S1-P2"

[25"G5-S1-P2" "G1-S2-P2" "G2-S2-P2" "G3-S2-P2" "G4-S2-P2" "G5-S2-P2"

[31"G1-S3-P2" "G2-S3-P2" "G3-S3-P2" "G4-S3-P2" "G5-S3-P2" "G1-S4-P2"

[37"G2-S4-P2" "G3-S4-P2" "G4-S4-P2" "G5-S4-P2" "G1-S1-P3" "G2-S1-P3"

[43"G3-S1-P3" "G4-S1-P3" "G5-S1-P3" "G1-S2-P3" "G2-S2-P3" "G3-S2-P3"

[49"G4-S2-P3" "G5-S2-P3" "G1-S3-P3" "G2-S3-P3" "G3-S3-P3" "G4-S3-P3"

[55"G5-S3-P3" "G1-S4-P3" "G2-S4-P3" "G3-S4-P3" "G4-S4-P3" "G5-S4-P3"

[61"G1-S1-P4" "G2-S1-P4" "G3-S1-P4" "G4-S1-P4" "G5-S1-P4" "G1-S2-P4"

[67"G2-S2-P4" "G3-S2-P4" "G4-S2-P4" "G5-S2-P4" "G1-S3-P4" "G2-S3-P4"

[73"G3-S3-P4" "G4-S3-P4" "G5-S3-P4" "G1-S4-P4" "G2-S4-P4" "G3-S4-P4"

[79"G4-S4-P4" "G5-S4-P4" "G1-S1-P5" "G2-S1-P5" "G3-S1-P5" "G4-S1-P5"

[85"G5-S1-P5" "G1-S2-P5" "G2-S2-P5" "G3-S2-P5" "G4-S2-P5" "G5-S2-P5"

[91"G1-S3-P5" "G2-S3-P5" "G3-S3-P5" "G4-S3-P5" "G5-S3-P5" "G1-S4-P5"

[97"G2-S4-P5" "G3-S4-P5" "G4-S4-P5" "G5-S4-P5"

例子4

x4<-c("Male","Female")

y4<-c("Tall","Short")

z4<-c("1000 to 2000","2001 to 5000","5001 to 10000",">10000")

a4<-c("Y1","Y2","Y3","Y4","Y5","Y6")

apply(expand.grid(x4,y4,z4,a4),1,paste,collapse="-")

输出结果

[1]  "Male-Tall-1000 to 2000-Y1" "Female-Tall-1000 to 2000-Y1"

[3]  "Male-Short-1000 to 2000-Y1" "Female-Short-1000 to 2000-Y1"

[5]  "Male-Tall-2001 to 5000-Y1" "Female-Tall-2001 to 5000-Y1"

[7]  "Male-Short-2001 to 5000-Y1" "Female-Short-2001 to 5000-Y1"

[9]  "Male-Tall-5001 to 10000-Y1" "Female-Tall-5001 to 10000-Y1"

[11"Male-Short-5001 to 10000-Y1" "Female-Short-5001 to 10000-Y1"

[13"Male-Tall->10000-Y1" "Female-Tall->10000-Y1"

[15"Male-Short->10000-Y1" "Female-Short->10000-Y1"

[17"Male-Tall-1000 to 2000-Y2" "Female-Tall-1000 to 2000-Y2"

[19"Male-Short-1000 to 2000-Y2" "Female-Short-1000 to 2000-Y2"

[21"Male-Tall-2001 to 5000-Y2" "Female-Tall-2001 to 5000-Y2"

[23"Male-Short-2001 to 5000-Y2" "Female-Short-2001 to 5000-Y2"

[25"Male-Tall-5001 to 10000-Y2" "Female-Tall-5001 to 10000-Y2"

[27"Male-Short-5001 to 10000-Y2" "Female-Short-5001 to 10000-Y2"

[29"Male-Tall->10000-Y2" "Female-Tall->10000-Y2"

[31"Male-Short->10000-Y2" "Female-Short->10000-Y2"

[33"Male-Tall-1000 to 2000-Y3" "Female-Tall-1000 to 2000-Y3"

[35"Male-Short-1000 to 2000-Y3" "Female-Short-1000 to 2000-Y3"

[37"Male-Tall-2001 to 5000-Y3" "Female-Tall-2001 to 5000-Y3"

[39"Male-Short-2001 to 5000-Y3" "Female-Short-2001 to 5000-Y3"

[41"Male-Tall-5001 to 10000-Y3" "Female-Tall-5001 to 10000-Y3"

[43"Male-Short-5001 to 10000-Y3" "Female-Short-5001 to 10000-Y3"

[45"Male-Tall->10000-Y3" "Female-Tall->10000-Y3"

[47"Male-Short->10000-Y3" "Female-Short->10000-Y3"

[49"Male-Tall-1000 to 2000-Y4" "Female-Tall-1000 to 2000-Y4"

[51"Male-Short-1000 to 2000-Y4" "Female-Short-1000 to 2000-Y4"

[53"Male-Tall-2001 to 5000-Y4" "Female-Tall-2001 to 5000-Y4"

[55"Male-Short-2001 to 5000-Y4" "Female-Short-2001 to 5000-Y4"

[57"Male-Tall-5001 to 10000-Y4" "Female-Tall-5001 to 10000-Y4"

[59"Male-Short-5001 to 10000-Y4" "Female-Short-5001 to 10000-Y4"

[61"Male-Tall->10000-Y4" "Female-Tall->10000-Y4"

[63"Male-Short->10000-Y4" "Female-Short->10000-Y4"

[65"Male-Tall-1000 to 2000-Y5" "Female-Tall-1000 to 2000-Y5"

[67"Male-Short-1000 to 2000-Y5" "Female-Short-1000 to 2000-Y5"

[69"Male-Tall-2001 to 5000-Y5" "Female-Tall-2001 to 5000-Y5"

[71"Male-Short-2001 to 5000-Y5" "Female-Short-2001 to 5000-Y5"

[73"Male-Tall-5001 to 10000-Y5" "Female-Tall-5001 to 10000-Y5"

[75"Male-Short-5001 to 10000-Y5" "Female-Short-5001 to 10000-Y5"

[77"Male-Tall->10000-Y5" "Female-Tall->10000-Y5"

[79"Male-Short->10000-Y5" "Female-Short->10000-Y5"

[81"Male-Tall-1000 to 2000-Y6" "Female-Tall-1000 to 2000-Y6"

[83"Male-Short-1000 to 2000-Y6" "Female-Short-1000 to 2000-Y6"

[85"Male-Tall-2001 to 5000-Y6" "Female-Tall-2001 to 5000-Y6"

[87"Male-Short-2001 to 5000-Y6" "Female-Short-2001 to 5000-Y6"

[89"Male-Tall-5001 to 10000-Y6" "Female-Tall-5001 to 10000-Y6"

[91"Male-Short-5001 to 10000-Y6" "Female-Short-5001 to 10000-Y6"

[93"Male-Tall->10000-Y6" "Female-Tall->10000-Y6"

[95"Male-Short->10000-Y6" "Female-Short->10000-Y6"

以上是 如何在R中连接以连字符分隔的字符串向量? 的全部内容, 来源链接: utcz.com/z/326426.html

回到顶部