如何用R结尾的数字创建字符串向量?
如果我们要创建一个分类矢量,其所有唯一值都表示末尾带有数字的字符串,那么paste0函数可以帮助我们实现这一点。例如,如果我们要创建ID最多为10的向量,例如ID1,ID2,ID3,ID4,ID5,ID6,ID7,ID8,ID9和ID10,则可以将其作为paste0(“ID”,seq(1:10))。
示例
x1<-paste0("A",seq(1:100)) x1
输出结果
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" [11] "A11" "A12" "A13" "A14" "A15" "A16" "A17" "A18" "A19" "A20" [21] "A21" "A22" "A23" "A24" "A25" "A26" "A27" "A28" "A29" "A30" [31] "A31" "A32" "A33" "A34" "A35" "A36" "A37" "A38" "A39" "A40" [41] "A41" "A42" "A43" "A44" "A45" "A46" "A47" "A48" "A49" "A50" [51] "A51" "A52" "A53" "A54" "A55" "A56" "A57" "A58" "A59" "A60" [61] "A61" "A62" "A63" "A64" "A65" "A66" "A67" "A68" "A69" "A70" [71] "A71" "A72" "A73" "A74" "A75" "A76" "A77" "A78" "A79" "A80" [81] "A81" "A82" "A83" "A84" "A85" "A86" "A87" "A88" "A89" "A90" [91] "A91" "A92" "A93" "A94" "A95" "A96" "A97" "A98" "A99" "A100"
示例
x2<-paste0("ABC",seq(1:100)) x2
输出结果
[1] "ABC1" "ABC2" "ABC3" "ABC4" "ABC5" "ABC6" "ABC7" "ABC8" [9] "ABC9" "ABC10" "ABC11" "ABC12" "ABC13" "ABC14" "ABC15" "ABC16" [17] "ABC17" "ABC18" "ABC19" "ABC20" "ABC21" "ABC22" "ABC23" "ABC24" [25] "ABC25" "ABC26" "ABC27" "ABC28" "ABC29" "ABC30" "ABC31" "ABC32" [33] "ABC33" "ABC34" "ABC35" "ABC36" "ABC37" "ABC38" "ABC39" "ABC40" [41] "ABC41" "ABC42" "ABC43" "ABC44" "ABC45" "ABC46" "ABC47" "ABC48" [49] "ABC49" "ABC50" "ABC51" "ABC52" "ABC53" "ABC54" "ABC55" "ABC56" [57] "ABC57" "ABC58" "ABC59" "ABC60" "ABC61" "ABC62" "ABC63" "ABC64" [65] "ABC65" "ABC66" "ABC67" "ABC68" "ABC69" "ABC70" "ABC71" "ABC72" [73] "ABC73" "ABC74" "ABC75" "ABC76" "ABC77" "ABC78" "ABC79" "ABC80" [81] "ABC81" "ABC82" "ABC83" "ABC84" "ABC85" "ABC86" "ABC87" "ABC88" [89] "ABC89" "ABC90" "ABC91" "ABC92" "ABC93" "ABC94" "ABC95" "ABC96" [97] "ABC97" "ABC98" "ABC99" "ABC100"
示例
x3<-paste0("ID",seq(1:100)) x3
输出结果
[1] "ID1" "ID2" "ID3" "ID4" "ID5" "ID6" "ID7" "ID8" "ID9" [10] "ID10" "ID11" "ID12" "ID13" "ID14" "ID15" "ID16" "ID17" "ID18" [19] "ID19" "ID20" "ID21" "ID22" "ID23" "ID24" "ID25" "ID26" "ID27" [28] "ID28" "ID29" "ID30" "ID31" "ID32" "ID33" "ID34" "ID35" "ID36" [37] "ID37" "ID38" "ID39" "ID40" "ID41" "ID42" "ID43" "ID44" "ID45" [46] "ID46" "ID47" "ID48" "ID49" "ID50" "ID51" "ID52" "ID53" "ID54" [55] "ID55" "ID56" "ID57" "ID58" "ID59" "ID60" "ID61" "ID62" "ID63" [64] "ID64" "ID65" "ID66" "ID67" "ID68" "ID69" "ID70" "ID71" "ID72" [73] "ID73" "ID74" "ID75" "ID76" "ID77" "ID78" "ID79" "ID80" "ID81" [82] "ID82" "ID83" "ID84" "ID85" "ID86" "ID87" "ID88" "ID89" "ID90" [91] "ID91" "ID92" "ID93" "ID94" "ID95" "ID96" "ID97" "ID98" "ID99" [100] "ID100"
示例
x4<-paste0("Level_",seq(1:100)) x4
输出结果
[1] "Level_1" "Level_2" "Level_3" "Level_4" "Level_5" "Level_6" [7] "Level_7" "Level_8" "Level_9" "Level_10" "Level_11" "Level_12" [13] "Level_13" "Level_14" "Level_15" "Level_16" "Level_17" "Level_18" [19] "Level_19" "Level_20" "Level_21" "Level_22" "Level_23" "Level_24" [25] "Level_25" "Level_26" "Level_27" "Level_28" "Level_29" "Level_30" [31] "Level_31" "Level_32" "Level_33" "Level_34" "Level_35" "Level_36" [37] "Level_37" "Level_38" "Level_39" "Level_40" "Level_41" "Level_42" [43] "Level_43" "Level_44" "Level_45" "Level_46" "Level_47" "Level_48" [49] "Level_49" "Level_50" "Level_51" "Level_52" "Level_53" "Level_54" [55] "Level_55" "Level_56" "Level_57" "Level_58" "Level_59" "Level_60" [61] "Level_61" "Level_62" "Level_63" "Level_64" "Level_65" "Level_66" [67] "Level_67" "Level_68" "Level_69" "Level_70" "Level_71" "Level_72" [73] "Level_73" "Level_74" "Level_75" "Level_76" "Level_77" "Level_78" [79] "Level_79" "Level_80" "Level_81" "Level_82" "Level_83" "Level_84" [85] "Level_85" "Level_86" "Level_87" "Level_88" "Level_89" "Level_90" [91] "Level_91" "Level_92" "Level_93" "Level_94" "Level_95" "Level_96" [97] "Level_97" "Level_98" "Level_99" "Level_100"
示例
x5<-paste0("S.No-",seq(1:100)) x5
输出结果
[1] "S.No-1" "S.No-2" "S.No-3" "S.No-4" "S.No-5" "S.No-6" [7] "S.No-7" "S.No-8" "S.No-9" "S.No-10" "S.No-11" "S.No-12" [13] "S.No-13" "S.No-14" "S.No-15" "S.No-16" "S.No-17" "S.No-18" [19] "S.No-19" "S.No-20" "S.No-21" "S.No-22" "S.No-23" "S.No-24" [25] "S.No-25" "S.No-26" "S.No-27" "S.No-28" "S.No-29" "S.No-30" [31] "S.No-31" "S.No-32" "S.No-33" "S.No-34" "S.No-35" "S.No-36" [37] "S.No-37" "S.No-38" "S.No-39" "S.No-40" "S.No-41" "S.No-42" [43] "S.No-43" "S.No-44" "S.No-45" "S.No-46" "S.No-47" "S.No-48" [49] "S.No-49" "S.No-50" "S.No-51" "S.No-52" "S.No-53" "S.No-54" [55] "S.No-55" "S.No-56" "S.No-57" "S.No-58" "S.No-59" "S.No-60" [61] "S.No-61" "S.No-62" "S.No-63" "S.No-64" "S.No-65" "S.No-66" [67] "S.No-67" "S.No-68" "S.No-69" "S.No-70" "S.No-71" "S.No-72" [73] "S.No-73" "S.No-74" "S.No-75" "S.No-76" "S.No-77" "S.No-78" [79] "S.No-79" "S.No-80" "S.No-81" "S.No-82" "S.No-83" "S.No-84" [85] "S.No-85" "S.No-86" "S.No-87" "S.No-88" "S.No-89" "S.No-90" [91] "S.No-91" "S.No-92" "S.No-93" "S.No-94" "S.No-95" "S.No-96" [97] "S.No-97" "S.No-98" "S.No-99" "S.No-100"
示例
x6<-paste0("Group:",seq(1:100)) x6
输出结果
[1] "Group:1" "Group:2" "Group:3" "Group:4" "Group:5" "Group:6" [7] "Group:7" "Group:8" "Group:9" "Group:10" "Group:11" "Group:12" [13] "Group:13" "Group:14" "Group:15" "Group:16" "Group:17" "Group:18" [19] "Group:19" "Group:20" "Group:21" "Group:22" "Group:23" "Group:24" [25] "Group:25" "Group:26" "Group:27" "Group:28" "Group:29" "Group:30" [31] "Group:31" "Group:32" "Group:33" "Group:34" "Group:35" "Group:36" [37] "Group:37" "Group:38" "Group:39" "Group:40" "Group:41" "Group:42" [43] "Group:43" "Group:44" "Group:45" "Group:46" "Group:47" "Group:48" [49] "Group:49" "Group:50" "Group:51" "Group:52" "Group:53" "Group:54" [55] "Group:55" "Group:56" "Group:57" "Group:58" "Group:59" "Group:60" [61] "Group:61" "Group:62" "Group:63" "Group:64" "Group:65" "Group:66" [67] "Group:67" "Group:68" "Group:69" "Group:70" "Group:71" "Group:72" [73] "Group:73" "Group:74" "Group:75" "Group:76" "Group:77" "Group:78" [79] "Group:79" "Group:80" "Group:81" "Group:82" "Group:83" "Group:84" [85] "Group:85" "Group:86" "Group:87" "Group:88" "Group:89" "Group:90" [91] "Group:91" "Group:92" "Group:93" "Group:94" "Group:95" "Group:96" [97] "Group:97" "Group:98" "Group:99" "Group:100"
示例
x7<-paste0("Class#",seq(1:100)) x7
输出结果
[1] "Class#1" "Class#2" "Class#3" "Class#4" "Class#5" "Class#6" [7] "Class#7" "Class#8" "Class#9" "Class#10" "Class#11" "Class#12" [13] "Class#13" "Class#14" "Class#15" "Class#16" "Class#17" "Class#18" [19] "Class#19" "Class#20" "Class#21" "Class#22" "Class#23" "Class#24" [25] "Class#25" "Class#26" "Class#27" "Class#28" "Class#29" "Class#30" [31] "Class#31" "Class#32" "Class#33" "Class#34" "Class#35" "Class#36" [37] "Class#37" "Class#38" "Class#39" "Class#40" "Class#41" "Class#42" [43] "Class#43" "Class#44" "Class#45" "Class#46" "Class#47" "Class#48" [49] "Class#49" "Class#50" "Class#51" "Class#52" "Class#53" "Class#54" [55] "Class#55" "Class#56" "Class#57" "Class#58" "Class#59" "Class#60" [61] "Class#61" "Class#62" "Class#63" "Class#64" "Class#65" "Class#66" [67] "Class#67" "Class#68" "Class#69" "Class#70" "Class#71" "Class#72" [73] "Class#73" "Class#74" "Class#75" "Class#76" "Class#77" "Class#78" [79] "Class#79" "Class#80" "Class#81" "Class#82" "Class#83" "Class#84" [85] "Class#85" "Class#86" "Class#87" "Class#88" "Class#89" "Class#90" [91] "Class#91" "Class#92" "Class#93" "Class#94" "Class#95" "Class#96" [97] "Class#97" "Class#98" "Class#99" "Class#100"
示例
x8<-paste0("SSN",seq(1:100)) x8
输出结果
[1] "SSN1" "SSN2" "SSN3" "SSN4" "SSN5" "SSN6" "SSN7" "SSN8" [9] "SSN9" "SSN10" "SSN11" "SSN12" "SSN13" "SSN14" "SSN15" "SSN16" [17] "SSN17" "SSN18" "SSN19" "SSN20" "SSN21" "SSN22" "SSN23" "SSN24" [25] "SSN25" "SSN26" "SSN27" "SSN28" "SSN29" "SSN30" "SSN31" "SSN32" [33] "SSN33" "SSN34" "SSN35" "SSN36" "SSN37" "SSN38" "SSN39" "SSN40" [41] "SSN41" "SSN42" "SSN43" "SSN44" "SSN45" "SSN46" "SSN47" "SSN48" [49] "SSN49" "SSN50" "SSN51" "SSN52" "SSN53" "SSN54" "SSN55" "SSN56" [57] "SSN57" "SSN58" "SSN59" "SSN60" "SSN61" "SSN62" "SSN63" "SSN64" [65] "SSN65" "SSN66" "SSN67" "SSN68" "SSN69" "SSN70" "SSN71" "SSN72" [73] "SSN73" "SSN74" "SSN75" "SSN76" "SSN77" "SSN78" "SSN79" "SSN80" [81] "SSN81" "SSN82" "SSN83" "SSN84" "SSN85" "SSN86" "SSN87" "SSN88" [89] "SSN89" "SSN90" "SSN91" "SSN92" "SSN93" "SSN94" "SSN95" "SSN96" [97] "SSN97" "SSN98" "SSN99" "SSN100"