

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 concatenate string vectors separated with hyphen in R?
The concatenation of string vectors will create combination of the values in the vectors thus, we can use them for interaction between/among the vectors. In R, we can use expand.grid along with apply to create such type of combinations as shown in the below examples.
Example 1
x1<-c("India","Russia","China") y1<-c("UK","USA","Canada") apply(expand.grid(x1,y1),1,paste,collapse="-")
Output
[1] "India-UK" "Russia-UK" "China-UK" "India-USA" [5] "Russia-USA" "China-USA" "India-Canada" "Russia-Canada" [9] "China-Canada"
Example 2
x2<-c("Hot","Cold") y2<-c("Summer","Winter","Spring") apply(expand.grid(x2,y2),1,paste,collapse="-")
Output
[1] "Hot-Summer" "Cold-Summer" "Hot-Winter" "Cold-Winter" "Hot-Spring" [6] "Cold-Spring"
Example 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="-")
Output
[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"
Example 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="-")
Output
[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"
- Related Questions & Answers
- How to concatenate numerical vectors and a string to return a string in R?
- How to concatenate two or more vectors in R?
- MySQL left padding with zeros separated by hyphen?
- Combine values of two columns separated with hyphen in an R data frame.
- How to concatenate elements of equal size vectors alternatively in R?
- How to print string vectors vertically in R?
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- REGEX in MySQL to display only numbers separated by hyphen.
- Hyphen string to camelCase string in JavaScript
- How to find different elements between two string vectors in R?
- How to concatenate strings in R?
- How to concatenate a string with numbers in Python?
- How to remove spaces at the end in string vectors in R?
- How to sum a comma separated string (string with numbers) in MySQL?
Advertisements