如何在 R 中反转数字?


要反转一个数字,我们可以使用 stringi 软件包的 stri_reverse 函数。例如,如果我们有一个包含一些数字的向量 x,那么通过使用命令 stri_reverse(x) 就可以反转这些数字。但是结果将以字符形式显示,如果我们想要得到数字值,则可以使用 as.numeric 函数。

library(stringi)

示例

 动态演示

x1<-sample(11:99,150,replace=TRUE)
x1

输出

  [1] 81 85 58 86 24 64 87 98 58 20 52 44 92 76 23 66 34 38 65 98 15 43 80 18 68
 [26] 18 36 53 74 74 46 38 81 58 49 46 54 76 47 94 18 38 79 79 76 40 98 84 81 35
 [51] 18 73 24 80 38 86 85 75 56 14 87 82 68 42 12 50 79 49 21 12 25 34 29 91 53
 [76] 24 99 59 92 88 82 53 16 15 55 65 77 86 49 56 37 42 17 11 90 25 46 11 62 99
[101] 68 32 28 30 29 91 99 50 65 53 99 65 21 91 33 40 92 20 90 81 23 97 31 22 96
[126] 65 23 11 21 30 44 87 34 98 76 15 30 20 47 45 71 93 75 55 56 79 56 29 80 72

示例

stri_reverse(x1)

输出

  [1] "18" "58" "85" "68" "42" "46" "78" "89" "85" "02" "25" "44" "29" "67" "32"
 [16] "66" "43" "83" "56" "89" "51" "34" "08" "81" "86" "81" "63" "35" "47" "47"
 [31] "64" "83" "18" "85" "94" "64" "45" "67" "74" "49" "81" "83" "97" "97" "67"
 [46] "04" "89" "48" "18" "53" "81" "37" "42" "08" "83" "68" "58" "57" "65" "41"
 [61] "78" "28" "86" "24" "21" "05" "97" "94" "12" "21" "52" "43" "92" "19" "35"
 [76] "42" "99" "95" "29" "88" "28" "35" "61" "51" "55" "56" "77" "68" "94" "65"
 [91] "73" "24" "71" "11" "09" "52" "64" "11" "26" "99" "86" "23" "82" "03" "92"
[106] "19" "99" "05" "56" "35" "99" "56" "12" "19" "33" "04" "29" "02" "09" "18"
[121] "32" "79" "13" "22" "69" "56" "32" "11" "12" "03" "44" "78" "43" "89" "67"
[136] "51" "03" "02" "74" "54" "17" "39" "57" "55" "65" "97" "65" "92" "08" "27"

示例

 动态演示

x2<-sample(11:999,150,replace=TRUE)
x2

输出

  [1] 954 378 972 913 565 367 309 146 970 576 211 758 645 791 582 97 733 218
 [19] 669 390 333 281 648 387 359 795 21 222 394 962 709 11 378 131 851 117
 [37] 908 776 642 631 641 914 151 835 406 166 259 721 497 963 756 873 834 477
 [55] 271 983 670 645 714 546 451 316 288 62 584 441 989 785 898 246 440 630
 [73] 229 412 919 385 288 769 743 177 473 838 579 488 671 836 196 917 87 880
 [91] 719 192 379 871 847 588 445 394 362 227 506 366 67 328 473 679 836 402
[109] 204 20 926 696 892 43 335 934 83 782 187 648 610 248 806 538 640 956
[127] 911 685 76 86 86 499 201 634 410 307 315 469 477 780 21 380 996 968
[145] 288 757 292 963 252 419

示例

stri_reverse(x2)

输出

  [1] "459" "873" "279" "319" "565" "763" "903" "641" "079" "675" "112" "857"
 [13] "546" "197" "285" "79" "337" "812" "966" "093" "333" "182" "846" "783"
 [25] "953" "597" "12" "222" "493" "269" "907" "11" "873" "131" "158" "711"
 [37] "809" "677" "246" "136" "146" "419" "151" "538" "604" "661" "952" "127"
 [49] "794" "369" "657" "378" "438" "774" "172" "389" "076" "546" "417" "645"
 [61] "154" "613" "882" "26" "485" "144" "989" "587" "898" "642" "044" "036"
 [73] "922" "214" "919" "583" "882" "967" "347" "771" "374" "838" "975" "884"
 [85] "176" "638" "691" "719" "78" "088" "917" "291" "973" "178" "748" "885"
 [97] "544" "493" "263" "722" "605" "663" "76" "823" "374" "976" "638" "204"
[109] "402" "02" "629" "696" "298" "34" "533" "439" "38" "287" "781" "846"
[121] "016" "842" "608" "835" "046" "659" "119" "586" "67" "68" "68" "994"
[133] "102" "436" "014" "703" "513" "964" "774" "087" "12" "083" "699" "869"
[145] "882" "757" "292" "369" "252" "914"

示例

 动态演示

x3<-sample(1001:99999,150,replace=TRUE)
x3

输出

  [1] 73622 34288 38938 28377 83212 29627 30921 25333 19601 62676 70937 55502
 [13] 21389 14570 30746 17836 87447 22853 25044 94783 48307 50670 70627 4562
 [25] 96789 69912 41967 18643 1035 88244 17401 64488 13720 31449 18942 52366
 [37] 6824 18589 76145 31281 45420 99839 69743 84190 99704 14902 32773 8953
 [49] 14993 71962 57506 19207 1674 75784 24456 85607 84580 49689 71915 81769
 [61] 32098 16524 60403 20146 55059 10099 97249 50647 88829 49614 38559 30852
 [73] 7149 11713 73396 6702 23879 20067 71224 26762 49158 92822 86376 57787
 [85] 32718 61627 27283 62469 68702 77628 89122 64613 90334 55600 15652 36349
 [97] 82832 20806 63575 67595 14961 14163 62328 4469 57012 28760 73848 36023
[109] 76810 13240 77951 53367 65150 25707 76103 65388 77243 83099 35606 85419
[121] 91350 1493 23704 41596 37523 59709 95094 69194 49730 91611 64213 89254
[133] 49197 20531 62349 28989 60663 18739 92706 38893 42033 16024 12930 64088
[145] 41299 32326 73859 2241 68530 80915

示例

stri_reverse(x3)

输出

[1] "22637" "88243" "83983" "77382" "21238" "72692" "12903" "33352" "10691"
[10] "67626" "73907" "20555" "98312" "07541" "64703" "63871" "74478" "35822"
[19] "44052" "38749" "70384" "07605" "72607" "2654" "98769" "21996" "76914"
[28] "34681" "5301" "44288" "10471" "88446" "02731" "94413" "24981" "66325"
[37] "4286" "98581" "54167" "18213" "02454" "93899" "34796" "09148" "40799"
[46] "20941" "37723" "3598" "39941" "26917" "60575" "70291" "4761" "48757"
[55] "65442" "70658" "08548" "98694" "51917" "96718" "89023" "42561" "30406"
[64] "64102" "95055" "99001" "94279" "74605" "92888" "41694" "95583" "25803"
[73] "9417" "31711" "69337" "2076" "97832" "76002" "42217" "26762" "85194"
[82] "22829" "67368" "78775" "81723" "72616" "38272" "96426" "20786" "82677"
[91] "22198" "31646" "43309" "00655" "25651" "94363" "23828" "60802" "57536"
[100] "59576" "16941" "36141" "82326" "9644" "21075" "06782" "84837" "32063"
[109] "01867" "04231" "15977" "76335" "05156" "70752" "30167" "88356" "34277"
[118] "99038" "60653" "91458" "05319" "3941" "40732" "69514" "32573" "90795"
[127] "49059" "49196" "03794" "11619" "31246" "45298" "79194" "13502" "94326"
[136] "98982" "36606" "93781" "60729" "39883" "33024" "42061" "03921" "88046"
[145] "99214" "62323" "95837" "1422" "03586" "51908"

示例

 动态演示

x4<-sample(0:100,150,replace=TRUE)
x4

输出

  [1] 10 84 59 74 58 51 45 63 90 39 29 20 55 1 98 92 11 9
 [19] 92 37 45 63 58 35 100 56 98 53 28 69 31 50 10 4 15 1
 [37] 40 47 94 75 77 36 48 87 100 7 42 52 29 48 53 29 9 12
 [55] 16 88 60 55 24 39 47 74 11 54 95 66 94 22 62 62 71 35
 [73] 77 59 86 15 69 59 6 73 24 12 50 45 36 22 73 40 55 42
 [91] 74 26 97 23 36 0 12 50 55 98 96 68 37 30 79 38 76 73
[109] 37 52 29 92 9 83 44 5 58 79 99 53 63 62 48 61 37 74
[127] 53 72 40 90 61 51 96 86 47 50 73 38 71 25 19 32 18 38
[145] 14 72 88 90 84 51

示例

stri_reverse(x4)

输出

  [1] "01" "48" "95" "47" "85" "15" "54" "36" "09" "93" "92" "02"
 [13] "55" "1" "89" "29" "11" "9" "29" "73" "54" "36" "85" "53"
 [25] "001" "65" "89" "35" "82" "96" "13" "05" "01" "4" "51" "1"
 [37] "04" "74" "49" "57" "77" "63" "84" "78" "001" "7" "24" "25"
 [49] "92" "84" "35" "92" "9" "21" "61" "88" "06" "55" "42" "93"
 [61] "74" "47" "11" "45" "59" "66" "49" "22" "26" "26" "17" "53"
 [73] "77" "95" "68" "51" "96" "95" "6" "37" "42" "21" "05" "54"
 [85] "63" "22" "37" "04" "55" "24" "47" "62" "79" "32" "63" "0"
 [97] "21" "05" "55" "89" "69" "86" "73" "03" "97" "83" "67" "37"
[109] "73" "25" "92" "29" "9" "38" "44" "5" "85" "97" "99" "35"
[121] "36" "26" "84" "16" "73" "47" "35" "27" "04" "09" "16" "15"
[133] "69" "68" "74" "05" "37" "83" "17" "52" "91" "23" "81" "83"
[145] "41" "27" "88" "09" "48" "15"

示例

 动态演示

x5<-round(rnorm(150,25,3.2),0)
x5

输出

  [1] 27 27 24 23 27 28 21 26 30 20 26 25 26 30 25 23 28 24 24 29 23 28 24 26 23
 [26] 28 17 20 20 26 24 23 31 28 28 19 21 25 25 26 28 29 31 23 25 30 22 22 22 21
 [51] 29 25 26 27 26 29 26 22 25 24 29 23 23 19 27 23 24 25 27 32 26 23 22 28 25
 [76] 17 31 21 23 26 17 28 24 26 25 22 27 24 26 24 26 25 25 24 25 21 18 25 25 21
[101] 27 28 23 17 22 24 21 22 19 23 17 26 25 29 26 28 27 24 19 26 22 23 25 24 26
[126] 26 24 27 20 23 29 31 26 28 22 31 30 27 25 23 25 21 24 23 26 24 23 20 23 22

示例

stri_reverse(x5)

输出

  [1] "72" "72" "42" "32" "72" "82" "12" "62" "03" "02" "62" "52" "62" "03" "52"
 [16] "32" "82" "42" "42" "92" "32" "82" "42" "62" "32" "82" "71" "02" "02" "62"
 [31] "42" "32" "13" "82" "82" "91" "12" "52" "52" "62" "82" "92" "13" "32" "52"
 [46] "03" "22" "22" "22" "12" "92" "52" "62" "72" "62" "92" "62" "22" "52" "42"
 [61] "92" "32" "32" "91" "72" "32" "42" "52" "72" "23" "62" "32" "22" "82" "52"
 [76] "71" "13" "12" "32" "62" "71" "82" "42" "62" "52" "22" "72" "42" "62" "42"
 [91] "62" "52" "52" "42" "52" "12" "81" "52" "52" "12" "72" "82" "32" "71" "22"
[106] "42" "12" "22" "91" "32" "71" "62" "52" "92" "62" "82" "72" "42" "91" "62"
[121] "22" "32" "52" "42" "62" "62" "42" "72" "02" "32" "92" "13" "62" "82" "22"
[136] "13" "03" "72" "52" "32" "52" "12" "42" "32" "62" "42" "32" "02" "32" "22"

更新时间:2021-02-11

2K+ 浏览

启动你的职业生涯

通过完成课程取得资格认证

开始学习
广告