如何连接数值向量和字符串以返回R中的字符串?
通常,数字向量和字符串的连接会导致R中的字符串向量。例如,如果我们要使用粘贴功能将1、2、3与Nhooo串联,那么它将得到一个矢量:“ Nhooo 1” “ Nhooo 2”“ Nhooo 3”。但是,如果我们希望它返回为“ Nhooo 1 2 3”,则需要使用带有粘贴功能的崩溃参数。
例1
> x1
输出结果
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
> y1
输出结果
[1] 1 9 6 3 8 1 8 5 9 1 2 8 3 8 4 5 10 1 6 3 9 8 2 2 3[26] 7 5 5 1 5 7 10 5 4 8 7 6 4 5 4 10 8 4 7 1 1 3 9 2 3
> paste(c(x1,y1),collapse="")
输出结果
[1] "ABCDEFGHIJ19638185912838451016398223755157105487645410847113923"
例2
> x2<-"nhooo"> x2
[1] "nhooo"
> y2<-rnorm(50)> y2
输出结果
[1] -0.56450438 -0.87409683 -0.45545334 -1.92794565 -0.46204738 1.57181642[7] -0.98986601 0.79975012 1.03634852 0.40356248 -0.34070839 -0.92796566
[13] 0.77889388 -1.29986590 0.26185934 0.36384901 1.28980906 0.13354925
[19] 0.51731334 0.26331224 -1.12768065 1.00102322 0.14566609 -1.79023868
[25] 0.56832540 -0.30686295 1.42981171 -2.22294595 -0.13630659 -1.66825359
[31] -0.90014165 1.30602557 -0.60606615 -0.08894799 -0.36974716 1.74791307
[37] -1.06738971 0.55717065 0.44214527 1.37510547 -0.33059281 0.09155202
[43] -0.52415542 0.12080025 0.52308841 1.23751663 0.04092456 0.45909642
[49] -0.92586032 1.88577081
> paste(c(x2,y2),collapse="")
输出结果
[1] "nhooo-0.564504382978389-0.87409682529276-0.455453344480808-1.92794564994394-0.4620473763837751.57181641618727-
0.989866012956110.7997501231403411.03634851576550.403562482080398-
0.340708391515938-0.9279656644528410.778893880606269-
1.299865900879470.2618593419490970.3638490122278811.28980906119370.1335492
515560880.5173133396863840.263312236133718-
1.127680650369891.001023218744370.145666090450948-
1.790238680828030.568325403613886-0.306862949111771.42981170973405-
2.22294594835457-0.136306593294414-1.66825359470097-
0.9001416508373761.30602557188284-0.606066145406716-0.0889479853435373-
0.3697471568802411.74791306928163-
1.067389713621220.5571706471840320.4421452735967551.37510547035707-
0.3305928129508030.0915520241487435-
0.5241554231974670.1208002530584250.5230884092939871.237516628051970.04092
456035507360.459096416150022-0.9258603224075091.88577080883107"
例子3
> x3<-"nhooo"> y3<-1:10
> paste(c(x3,y3),collapse="")
输出结果
[1] "nhooo12345678910"
例子4
> x4<-"E-learning"> x4
输出结果
[1] "E-learning"
> y4<-rpois(50,5)> y4
输出结果
[1] 5 6 3 7 6 3 7 7 6 4 5 3 5 7 4 5 5 7 2 5 1 3 7 10 6[26] 2 3 3 5 6 4 3 3 2 9 7 5 6 6 4 5 4 8 5 4 5 8 5 10 9
> paste(c(x4,y4),collapse="")
输出结果
[1] "E-learning5637637764535745572513710623356433297566454854585109"
范例5
> x5<-"Simply-Easy-Learning"> x5
输出结果
[1] "Simply-Easy-Learning"
> y5<-sample(1:5,50,replace=TRUE)> y5
输出结果
[1] 5 5 3 3 3 1 5 4 4 1 2 1 4 4 5 5 1 3 1 3 2 3 2 2 3 3 4 1 5 1 2 5 2 5 5 4 4 1[39] 3 4 2 5 1 1 3 5 2 3 2 1
> paste(c(x4,y4),collapse=" ")
输出结果
[1] "E-learning 5 6 3 7 6 3 7 7 6 4 5 3 5 7 4 5 5 7 2 5 1 3 7 10 6 2 3 3 5 6 4 3 3 2 9 7 5 6 64 5 4 8 5 4 5 8 5 10 9"
范例6
> x6<-"Programming"> x6
输出结果
[1] "Programming"
> y6<-rpois(100,5)> y6
输出结果
[1] 3 5 3 5 7 7 3 3 4 9 4 3 2 8 3 3 7 5 5 2 6 4 2 4 3[26] 5 5 3 4 7 10 3 6 6 6 4 4 10 1 4 6 4 8 7 8 4 8 6 3 6
[51] 1 6 2 7 4 5 4 4 1 5 5 5 4 5 4 6 5 6 3 6 4 3 8 3 5
[76] 2 3 2 4 5 1 2 2 5 4 4 7 7 4 9 3 6 6 2 2 11 2 7 3 6
> paste(c(x6,y6),collapse="-")
输出结果
[1] "Programming-3-5-3-5-7-7-3-3-4-9-4-3-2-8-3-3-7-5-5-2-6-4-2-4-3-5-5-3-4-7-10-3-6-6-6-4-4-10-1-4-6-4-8-7-8-4-8-6-3-6-1-6-2-7-4-5-4-4-1-5-5-5-4-5-4-6-5-6-3-6-4-3-8-3-5-
2-3-2-4-5-1-2-2-5-4-4-7-7-4-9-3-6-6-2-2-11-2-7-3-6"
以上是 如何连接数值向量和字符串以返回R中的字符串? 的全部内容, 来源链接: utcz.com/z/351460.html