在 R 中创建数值向量,初始值为零,并定义每个值的最大位数。
为了在 R 中创建数值向量,初始值为零,并定义每个值的最大位数,我们可以使用 sprintf 函数。
例如,如果我们想创建一个数值向量,其值从 1 到 100,并且我们希望 1 为 001,依此类推,那么我们可以使用以下命令:
sprintf('%0.3d',1:100)
示例
以下代码片段创建了一个示例数据框:
x1<-sprintf('%0.3d',1:10) x1
如果执行上述代码片段,将生成以下输出:
[1] "001" "002" "003" "004" "005" "006" "007" "008" "009" "010"
在上述代码片段中添加以下代码:
x1<-sprintf('%0.3d',1:10) x2<-sprintf('%0.3d',sample(1:10,200,replace=TRUE)) x2
如果将上述所有代码片段作为一个程序执行,则会生成以下输出:
[1] "004" "007" "004" "007" "004" "008" "002" "004" "007" "007" "008" "009" [13] "008" "006" "008" "006" "009" "003" "002" "008" "006" "008" "005" "001" [25] "008" "008" "005" "001" "008" "002" "001" "007" "008" "003" "007" "006" [37] "009" "010" "009" "006" "002" "002" "004" "001" "008" "002" "009" "003" [49] "005" "003" "003" "003" "007" "004" "001" "006" "002" "003" "009" "006" [61] "009" "007" "004" "003" "003" "009" "003" "001" "007" "009" "007" "002" [73] "006" "001" "005" "005" "010" "007" "002" "008" "003" "009" "004" "003" [85] "010" "006" "003" "009" "002" "004" "008" "001" "007" "002" "001" "004" [97] "004" "003" "004" "001" "009" "003" "005" "007" "008" "009" "008" "007" [109] "006" "009" "008" "007" "005" "006" "003" "004" "006" "007" "009" "001" [121] "006" "007" "004" "002" "006" "001" "002" "003" "001" "004" "002" "004" [133] "002" "009" "009" "002" "004" "004" "010" "010" "001" "005" "007" "008" [145] "010" "003" "003" "002" "004" "003" "002" "002" "010" "004" "005" "005" [157] "005" "006" "001" "006" "009" "007" "010" "008" "003" "006" "004" "003" [169] "003" "009" "009" "003" "002" "008" "007" "005" "003" "004" "001" "006" [181] "010" "009" "002" "008" "005" "006" "008" "001" "004" "007" "002" "010" [193] "007" "005" "002" "007" "005" "003" "010" "003"
在上述代码片段中添加以下代码:
x1<-sprintf('%0.3d',1:10) x2<-sprintf('%0.3d',sample(1:10,200,replace=TRUE)) x3<-sprintf('%0.3d',sample(1:50,150,replace=TRUE)) x3
如果将上述所有代码片段作为一个程序执行,则会生成以下输出:
[1] "002" "033" "032" "017" "023" "029" "029" "047" "019" "028" "018" "032" [13] "013" "047" "032" "040" "045" "024" "024" "025" "001" "023" "005" "023" [25] "022" "039" "010" "043" "023" "004" "032" "011" "038" "031" "046" "006" [37] "015" "019" "047" "018" "023" "037" "010" "050" "024" "003" "005" "027" [49] "026" "049" "025" "031" "007" "036" "004" "008" "045" "040" "015" "016" [61] "041" "009" "041" "010" "015" "044" "028" "018" "011" "050" "007" "024" [73] "031" "035" "044" "029" "049" "010" "010" "049" "011" "014" "017" "041" [85] "021" "015" "014" "048" "011" "004" "049" "043" "010" "047" "048" "050" [97] "027" "028" "017" "004" "016" "001" "036" "010" "001" "035" "037" "017" [109] "037" "032" "047" "004" "047" "023" "029" "023" "023" "002" "003" "040" [121] "031" "025" "047" "008" "014" "007" "016" "002" "007" "028" "031" "026" [133] "034" "019" "011" "003" "040" "042" "034" "005" "003" "040" "025" "029" [145] "033" "033" "006" "038" "023" "036"
在上述代码片段中添加以下代码:
x1<-sprintf('%0.3d',1:10) x2<-sprintf('%0.3d',sample(1:10,200,replace=TRUE)) x3<-sprintf('%0.3d',sample(1:50,150,replace=TRUE)) x4<-sprintf('%0.3d',sample(1:99,150,replace=TRUE)) x4
如果将上述所有代码片段作为一个程序执行,则会生成以下输出:
[1] "073" "054" "091" "013" "043" "057" "057" "002" "068" "091" "079" "024" [13] "018" "038" "095" "038" "052" "060" "005" "001" "050" "065" "051" "016" [25] "055" "010" "081" "025" "010" "013" "053" "069" "074" "085" "029" "069" [37] "003" "003" "079" "021" "057" "076" "045" "057" "008" "040" "018" "002" [49] "021" "032" "058" "005" "020" "059" "047" "050" "081" "023" "031" "080" [61] "012" "013" "071" "011" "049" "091" "003" "044" "036" "079" "026" "013" [73] "070" "050" "050" "027" "065" "034" "040" "073" "030" "057" "030" "042" [85] "078" "098" "043" "053" "019" "036" "076" "094" "065" "068" "025" "004" [97] "041" "070" "002" "007" "033" "021" "057" "052" "058" "059" "083" "051" [109] "023" "063" "009" "002" "023" "040" "087" "022" "043" "014" "086" "073" [121] "090" "007" "013" "044" "067" "012" "083" "032" "016" "086" "077" "053" [133] "014" "079" "082" "068" "087" "032" "087" "094" "050" "055" "079" "063" [145] "068" "023" "002" "078" "005" "025"
在上述代码片段中添加以下代码:
x1<-sprintf('%0.3d',1:10) x2<-sprintf('%0.3d',sample(1:10,200,replace=TRUE)) x3<-sprintf('%0.3d',sample(1:50,150,replace=TRUE)) x4<-sprintf('%0.3d',sample(1:99,150,replace=TRUE)) x5<-sprintf('%0.3d',sample(1:99,99,replace=TRUE)) x5
如果将上述所有代码片段作为一个程序执行,则会生成以下输出:
[1] "034" "004" "017" "011" "064" "099" "078" "079" "061" "037" "040" "056" [13] "019" "005" "088" "012" "003" "081" "045" "021" "067" "053" "086" "076" [25] "093" "069" "049" "025" "096" "036" "067" "053" "063" "049" "041" "040" [37] "062" "037" "093" "051" "071" "050" "084" "033" "063" "068" "018" "035" [49] "089" "063" "025" "037" "015" "040" "067" "069" "056" "080" "098" "073" [61] "080" "046" "096" "086" "043" "006" "068" "028" "050" "088" "073" "069" [73] "021" "030" "058" "019" "021" "086" "005" "099" "049" "091" "019" "062" [85] "088" "076" "075" "092" "057" "014" "024" "030" "086" "087" "092" "067" [97] "004" "094" "053"
在上述代码片段中添加以下代码:
x1<-sprintf('%0.3d',1:10) x2<-sprintf('%0.3d',sample(1:10,200,replace=TRUE)) x3<-sprintf('%0.3d',sample(1:50,150,replace=TRUE)) x4<-sprintf('%0.3d',sample(1:99,150,replace=TRUE)) x5<-sprintf('%0.3d',sample(1:99,99,replace=TRUE)) x6<-sprintf('%0.4d',sample(990:1010,100,replace=TRUE)) x6
如果将上述所有代码片段作为一个程序执行,则会生成以下输出:
[1] "0990" "1000" "0998" "1009" "1001" "0992" "1008" "0995" "1001" "1009" [11] "1003" "1008" "1008" "0997" "1005" "1001" "1004" "1009" "1003" "0999" [21] "1004" "0994" "1002" "0995" "1006" "1003" "1010" "0992" "1009" "0998" [31] "0997" "0992" "0990" "0994" "1010" "0990" "1006" "1001" "1001" "1004" [41] "0990" "0994" "0998" "0998" "0997" "0998" "1000" "0996" "0995" "1009" [51] "0990" "1001" "0995" "0994" "1002" "1006" "1002" "0995" "0991" "1006" [61] "1000" "1003" "0994" "0990" "0994" "0991" "1001" "1004" "0992" "0997" [71] "1005" "0998" "1004" "1000" "0997" "1004" "0997" "0992" "0998" "1010" [81] "1008" "0992" "0994" "0992" "1006" "1001" "1003" "1000" "1002" "0993" [91] "1000" "0992" "1004" "1008" "1005" "0991" "1002" "1005" "1000" "0993"
在上述代码片段中添加以下代码:
x1<-sprintf('%0.3d',1:10) x2<-sprintf('%0.3d',sample(1:10,200,replace=TRUE)) x3<-sprintf('%0.3d',sample(1:50,150,replace=TRUE)) x4<-sprintf('%0.3d',sample(1:99,150,replace=TRUE)) x5<-sprintf('%0.3d',sample(1:99,99,replace=TRUE)) x6<-sprintf('%0.4d',sample(990:1010,100,replace=TRUE)) x7<-sprintf('%0.4d',sample(900:2000,100)) x7
如果将上述所有代码片段作为一个程序执行,则会生成以下输出:
[1] "1213" "0977" "0948" "1661" "1140" "1334" "1693" "1454" "1053" "1389" [11] "1090" "1666" "1528" "1815" "1771" "1513" "1094" "1924" "1846" "1149" [21] "1994" "1602" "1900" "1929" "1425" "1019" "1759" "1946" "1121" "0911" [31] "1197" "1124" "1727" "1394" "1374" "1439" "1031" "1979" "1247" "1206" [41] "1986" "1139" "1408" "1009" "1046" "0939" "1272" "1438" "1827" "1111" [51] "1903" "1880" "1118" "1742" "1720" "1973" "1568" "1962" "1689" "1026" [61] "1354" "1096" "0937" "1317" "1974" "1820" "1450" "1330" "1641" "1447" [71] "1834" "1143" "1844" "1608" "1709" "1564" "1542" "1569" "1193" "1430" [81] "1429" "1360" "1572" "1333" "1992" "1504" "1507" "1268" "1615" "1588" [91] "1490" "1308" "1280" "1361" "1467" "1895" "1743" "1818" "0909" "1840"
在上述代码片段中添加以下代码:
x1<-sprintf('%0.3d',1:10) x2<-sprintf('%0.3d',sample(1:10,200,replace=TRUE)) x3<-sprintf('%0.3d',sample(1:50,150,replace=TRUE)) x4<-sprintf('%0.3d',sample(1:99,150,replace=TRUE)) x5<-sprintf('%0.3d',sample(1:99,99,replace=TRUE)) x6<-sprintf('%0.4d',sample(990:1010,100,replace=TRUE)) x7<-sprintf('%0.4d',sample(900:2000,100)) x8<-sprintf('%0.5d',sample(9900:10100,100)) x8
如果将上述所有代码片段作为一个程序执行,则会生成以下输出:
[1] "10070" "10078" "09932" "10080" "10053" "09954" "10006" "10059" "10037" [10] "09978" "09979" "09986" "10067" "09919" "10046" "10049" "10022" "09929" [19] "09996" "10057" "10083" "09942" "10082" "10074" "10071" "09924" "09903" [28] "10024" "09910" "09975" "09918" "10005" "10055" "09990" "10010" "10075" [37] "09930" "09973" "09951" "10058" "09987" "10044" "10084" "10030" "09945" [46] "10079" "09999" "09941" "10099" "10097" "10040" "10031" "10051" "10018" [55] "09976" "09944" "09953" "09922" "10017" "09915" "09955" "09950" "10085" [64] "09939" "09927" "09957" "10028" "10045" "10032" "09946" "09991" "10029" [73] "09995" "10077" "10073" "09980" "10088" "09916" "10089" "09983" "09968" [82] "09912" "10056" "09904" "09969" "09913" "10009" "10094" "10093" "10016" [91] "10004" "09902" "09988" "09964" "09977" "10086" "09961" "10034" "10081" [100] "09960"
广告