How to concatenate numerical vectors and a string to return a string in R?

R ProgrammingServer Side ProgrammingProgramming

In general, the concatenation of numerical vectors and string results in a vector of strings in R. For example, if we want to concatenate 1, 2, 3 with Tutorialspoint using paste function then it will result in a vector as: "Tutorialspoint 1" "Tutorialspoint 2" "Tutorialspoint 3". But if we want it to return as "Tutorialspoint 1 2 3" then we need to use collapse argument with paste function.

Example1

> x1

Output

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
> y1

Output

[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="")

Output

[1] "ABCDEFGHIJ19638185912838451016398223755157105487645410847113923"

Example2

 Live Demo

> x2<-"TutorialsPoint"
> x2
[1] "TutorialsPoint"

 Live Demo

> y2<-rnorm(50)
> y2

Output

[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="")

Output

[1] "TutorialsPoint-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"

Example3

 Live Demo

> x3<-"TutorialsPoint"
> y3<-1:10
> paste(c(x3,y3),collapse="")

Output

[1] "TutorialsPoint12345678910"

Example4

 Live Demo

> x4<-"E-learning"
> x4

Output

[1] "E-learning"

 Live Demo

> y4<-rpois(50,5)
> y4

Output

[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="")

Output

[1] "E-learning5637637764535745572513710623356433297566454854585109"

Example5

 Live Demo

> x5<-"Simply-Easy-Learning"
> x5

Output

[1] "Simply-Easy-Learning"

 Live Demo

> y5<-sample(1:5,50,replace=TRUE)
> y5

Output

[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=" ")

Output

[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 6
4 5 4 8 5 4 5 8 5 10 9"

Example6

 Live Demo

> x6<-"Programming"
> x6

Output

[1] "Programming"

 Live Demo

> y6<-rpois(100,5)
> y6

Output

[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="-")

Output

[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"
raja
Published on 07-Sep-2020 06:06:43
Advertisements