How to combine two vectors by separating with different special characters in R?

R ProgrammingServer Side ProgrammingProgramming

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"
raja
Published on 12-Aug-2020 12:34:51
Advertisements