- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 combine two vectors by separating with different special characters in R?
The combination of two vectors is used for many purposes such as performing two-way ANOVA, presenting data table, or making visual representation of the data. The combinations can be created with many special characters in R by using paste and rep function.
Example
Consider the below vectors Class and Names.
> Class<-c("Stats","Maths","Chem","Physics","O-R") > Class [1] "Stats" "Maths" "Chem" "Physics" "O-R" > Names<-c(101,102,103,104,105) > Names [1] 101 102 103 104 105
Suppose we want to combine Class and Names in a way that the new vector contains Stats|101, Stats|102, and so on. Also, we want to do the same with different special characters.
We can do this by using paste and rep function with defining different separators as shown below −
> paste(rep(Class, each = length(Name)), Name, sep = "|") [1] "Stats|101" "Stats|102" "Stats|103" "Stats|104" "Stats|105" "Maths|101" "Maths|102" "Maths|103" "Maths|104" "Maths|105" "Chem|101" [12] "Chem|102" "Chem|103" "Chem|104" "Chem|105" "Physics|101" "Physics|102" "Physics|103" "Physics|104" "Physics|105" "O-R|101" "O-R|102" [23] "O-R|103" "O-R|104" "O-R|105" > paste(rep(Class, each = length(Name)), Name, sep = ".") [1] "Stats.101" "Stats.102" "Stats.103" "Stats.104" "Stats.105" "Maths.101" "Maths.102" "Maths.103" "Maths.104" "Maths.105" "Chem.101" [12] "Chem.102" "Chem.103" "Chem.104" "Chem.105" "Physics.101" "Physics.102" "Physics.103" "Physics.104" "Physics.105" "O-R.101" "O-R.102" [23] "O-R.103" "O-R.104" "O-R.105" > paste(rep(Class, each = length(Name)), Name, sep = "-") [1] "Stats-101" "Stats-102" "Stats-103" "Stats-104" "Stats-105" "Maths-101" "Maths-102" "Maths-103" "Maths-104" "Maths-105" "Chem-101" [12] "Chem-102" "Chem-103" "Chem-104" "Chem-105" "Physics-101" "Physics-102" "Physics-103" "Physics-104" "Physics-105" "O-R-101" "O-R-102" [23] "O-R-103" "O-R-104" "O-R-105" > paste(rep(Class, each = length(Name)), Name, sep = "&") [1] "Stats&101" "Stats&102" "Stats&103" "Stats&104" "Stats&105" "Maths&101" "Maths&102" "Maths&103" "Maths&104" "Maths&105" "Chem&101" [12] "Chem&102" "Chem&103" "Chem&104" "Chem&105" "Physics&101" "Physics&102" "Physics&103" "Physics&104" "Physics&105" "O-R&101" "O-R&102" [23] "O-R&103" "O-R&104" "O-R&105" > paste(rep(Class, each = length(Name)), Name, sep = "*") [1] "Stats*101" "Stats*102" "Stats*103" "Stats*104" "Stats*105" "Maths*101" "Maths*102" "Maths*103" "Maths*104" "Maths*105" "Chem*101" [12] "Chem*102" "Chem*103" "Chem*104" "Chem*105" "Physics*101" "Physics*102" "Physics*103" "Physics*104" "Physics*105" "O-R*101" "O-R*102" [23] "O-R*103" "O-R*104" "O-R*105" > paste(rep(Class, each = length(Name)), Name, sep = "@") [1] "Stats@101" "Stats@102" "Stats@103" "Stats@104" "Stats@105" "Maths@101" "Maths@102" "Maths@103" "Maths@104" "Maths@105" "Chem@101" [12] "Chem@102" "Chem@103" "Chem@104" "Chem@105" "Physics@101" "Physics@102" "Physics@103" "Physics@104" "Physics@105" "O-R@101" "O-R@102" [23] "O-R@103" "O-R@104" "O-R@105" > paste(rep(Class, each = length(Name)), Name, sep = "!") [1] "Stats!101" "Stats!102" "Stats!103" "Stats!104" "Stats!105" "Maths!101" "Maths!102" "Maths!103" "Maths!104" "Maths!105" "Chem!101" [12] "Chem!102" "Chem!103" "Chem!104" "Chem!105" "Physics!101" "Physics!102" "Physics!103" "Physics!104" "Physics!105" "O-R!101" "O-R!102" [23] "O-R!103" "O-R!104" "O-R!105" > paste(rep(Class, each = length(Name)), Name, sep = "/") [1] "Stats/101" "Stats/102" "Stats/103" "Stats/104" "Stats/105" "Maths/101" "Maths/102" "Maths/103" "Maths/104" "Maths/105" "Chem/101" [12] "Chem/102" "Chem/103" "Chem/104" "Chem/105" "Physics/101" "Physics/102" "Physics/103" "Physics/104" "Physics/105" "O-R/101" "O-R/102" [23] "O-R/103" "O-R/104" "O-R/105" > paste(rep(Class, each = length(Name)), Name, sep = "==") [1] "Stats==101" "Stats==102" "Stats==103" "Stats==104" "Stats==105" "Maths==101" "Maths==102" "Maths==103" "Maths==104" "Maths==105" [11] "Chem==101" "Chem==102" "Chem==103" "Chem==104" "Chem==105" "Physics==101" "Physics==102" "Physics==103" "Physics==104" "Physics==105" [21] "O-R==101" "O-R==102" "O-R==103" "O-R==104" "O-R==105" > paste(rep(Class, each = length(Name)), Name, sep = "#") [1] "Stats#101" "Stats#102" "Stats#103" "Stats#104" "Stats#105" "Maths#101" "Maths#102" "Maths#103" "Maths#104" "Maths#105" "Chem#101" [12] "Chem#102" "Chem#103" "Chem#104" "Chem#105" "Physics#101" "Physics#102" "Physics#103" "Physics#104" "Physics#105" "O-R#101" "O-R#102" [23] "O-R#103" "O-R#104" "O-R#105" > paste(rep(Class, each = length(Name)), Name, sep = "^") [1] "Stats^101" "Stats^102" "Stats^103" "Stats^104" "Stats^105" "Maths^101" "Maths^102" "Maths^103" "Maths^104" "Maths^105" "Chem^101" [12] "Chem^102" "Chem^103" "Chem^104" "Chem^105" "Physics^101" "Physics^102" "Physics^103" "Physics^104" "Physics^105" "O-R^101" "O-R^102" [23] "O-R^103" "O-R^104" "O-R^105" > paste(rep(Class, each = length(Name)), Name, sep = "_") [1] "Stats_101" "Stats_102" "Stats_103" "Stats_104" "Stats_105" "Maths_101" "Maths_102" "Maths_103" "Maths_104" "Maths_105" "Chem_101" [12] "Chem_102" "Chem_103" "Chem_104" "Chem_105" "Physics_101" "Physics_102" "Physics_103" "Physics_104" "Physics_105" "O-R_101" "O-R_102" [23] "O-R_103" "O-R_104" "O-R_105" > paste(rep(Class, each = length(Name)), Name, sep = ">") [1] "Stats>101" "Stats>102" "Stats>103" "Stats>104" "Stats>105" "Maths>101" "Maths>102" "Maths>103" "Maths>104" "Maths>105" "Chem>101" [12] "Chem>102" "Chem>103" "Chem>104" "Chem>105" "Physics>101" "Physics>102" "Physics>103" "Physics>104" "Physics>105" "O-R>101" "O-R>102" [23] "O-R>103" "O-R>104" "O-R>105" > paste(rep(Class, each = length(Name)), Name, sep = "+") [1] "Stats+101" "Stats+102" "Stats+103" "Stats+104" "Stats+105" "Maths+101" "Maths+102" "Maths+103" "Maths+104" "Maths+105" "Chem+101" [12] "Chem+102" "Chem+103" "Chem+104" "Chem+105" "Physics+101" "Physics+102" "Physics+103" "Physics+104" "Physics+105" "O-R+101" "O-R+102" [23] "O-R+103" "O-R+104" "O-R+105" > paste(rep(Class, each = length(Name)), Name, sep = "()") [1] "Stats()101" "Stats()102" "Stats()103" "Stats()104" "Stats()105" "Maths()101" "Maths()102" "Maths()103" "Maths()104" "Maths()105" [11] "Chem()101" "Chem()102" "Chem()103" "Chem()104" "Chem()105" "Physics()101" "Physics()102" "Physics()103" "Physics()104" "Physics()105" [21] "O-R()101" "O-R()102" "O-R()103" "O-R()104" "O-R()105" > paste(rep(Class, each = length(Name)), Name, sep = "~") [1] "Stats~101" "Stats~102" "Stats~103" "Stats~104" "Stats~105" "Maths~101" "Maths~102" "Maths~103" "Maths~104" "Maths~105" "Chem~101" [12] "Chem~102" "Chem~103" "Chem~104" "Chem~105" "Physics~101" "Physics~102" "Physics~103" "Physics~104" "Physics~105" "O-R~101" "O-R~102" [23] "O-R~103" "O-R~104" "O-R~105" > paste(rep(Class, each = length(Name)), Name, sep = "?") [1] "Stats?101" "Stats?102" "Stats?103" "Stats?104" "Stats?105" "Maths?101" "Maths?102" "Maths?103" "Maths?104" "Maths?105" "Chem?101" [12] "Chem?102" "Chem?103" "Chem?104" "Chem?105" "Physics?101" "Physics?102" "Physics?103" "Physics?104" "Physics?105" "O-R?101" "O-R?102" [23] "O-R?103" "O-R?104" "O-R?105" > paste(rep(Class, each = length(Name)), Name, sep = "<>") [1] "Stats<>101" "Stats<>102" "Stats<>103" "Stats<>104" "Stats<>105" "Maths<>101" "Maths<>102" "Maths<>103" "Maths<>104" "Maths<>105" [11] "Chem<>101" "Chem<>102" "Chem<>103" "Chem<>104" "Chem<>105" "Physics<>101" "Physics<>102" "Physics<>103" "Physics<>104" "Physics<>105" [21] "O-R<>101" "O-R<>102" "O-R<>103" "O-R<>104" "O-R<>105"
- Related Articles
- How to combine two factor vectors to create one in R?
- How to combine array of vectors in R?
- How to find different elements between two string vectors in R?
- How to combine two vectors while replacing the NA values with the values in the other vector in R?
- How to combine two rows in R matrix by addition?
- How to match two string vectors if the strings case is different in both the vectors in R?
- How to separate strings in R that are joined with special characters?
- How to count special characters in an R vector?
- How to combine two rows in R data frame by addition?
- How to combine two rows in data.table object in R by addition?
- How to combine list elements of different sizes in R?
- How to split string values that contain special characters in R?
- How to concatenate two or more vectors in R?
- How to create boxplot of vectors having different lengths in R?
- How to multiply two vectors in R as in mathematics?

Advertisements