如何在 R 中将两个向量使用不同特殊字符进行分隔合并?


组合两个向量用于多种目的,例如进行双向 ANOVA、展示数据表或对数据进行可视化表示。组合可以用多个 R 中的特殊字符创建,方法是使用 paste 和 rep 函数。

示例

考虑下面的 Class 和 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

假设我们要以一种方式组合 Class 和 Names 使得新向量包含 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"

更新于: 12-8-2020

112 次浏览

开启职业生涯

通过完成课程获得认证

开始使用
广告