两个向量的组合用于许多目的,例如执行双向ANOVA,显示数据表或对数据进行可视表示。通过使用粘贴和表示功能,可以在R中使用许多特殊字符创建组合。
考虑下面的向量类和名称。
> 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
假设我们要以新向量包含Stats | 101,Stats | 102等的方式组合类和名称。另外,我们希望对不同的特殊字符执行相同的操作。
我们可以通过使用paste和rep函数并定义不同的分隔符来做到这一点,如下所示-
> 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"