如何找到 R 向量中数量最大的 n 个值?


一个向量可能有成百上千个值,并且每个值可能相同或不同。值可以按组指定或随机选取,但具有几个相同的值。无论向量中的值为何,为了找到一些较大的值,我们需要按升序对向量进行排序,然后选择较大的值。

示例

> x1<-rnorm(50)
> x1
[1] -1.4447473195 3.2906645299 -0.4680055849 0.1611487482 -0.7715094280
[6] 0.4442103640 0.3702444686 0.0783124252 1.3476432299 1.0140576107
[11] -0.0968917066 0.4628821017 0.3102594626 -0.2946001275 0.1498108166
[16] -0.6002154305 0.5905382364 1.3892651534 0.1008921325 -0.6486318692
[21] -0.0562831933 -0.6887431711 0.4907512082 -0.3994662410 0.7827897030
[26] 0.5294704584 -1.3802965730 -0.6159076490 -0.0009408529 1.6182294859
[31] 0.2539617286 -1.9173056766 0.9534899983 -0.0849641284 0.0726055903
[36] 0.8863460104 0.2516331008 -0.0296849348 0.2086944365 0.1112500667
[41] -0.2346866849 -1.6693864582 -1.2794848686 0.4355160606 1.1611288668
[46] -0.4128822851 0.3113799053 0.4608703922 -0.4767432276 -0.4108699626
> tail(sort(x1),10)
[1] 0.5905382 0.7827897 0.8863460 0.9534900 1.0140576 1.1611289 1.3476432
[8] 1.3892652 1.6182295 3.2906645
> x2<-rpois(50,2)
> x2
[1] 3 0 2 3 1 3 2 3 5 1 0 5 3 2 1 2 1 2 4 2 3 2 2 5 3 3 0 1 3 3 1 1 2 1 0 2 1 1
[39] 3 4 0 1 2 2 1 5 2 5 1 6
> tail(sort(x2),10)
[1] 3 3 4 4 5 5 5 5 5 6
> x2<-rpois(50,2)
> x2
[1] 3 0 2 3 1 3 2 3 5 1 0 5 3 2 1 2 1 2 4 2 3 2 2 5 3 3 0 1 3 3 1 1 2 1 0 2 1 1
[39] 3 4 0 1 2 2 1 5 2 5 1 6
> tail(sort(x2),10)
[1] 3 3 4 4 5 5 5 5 5 6
> x3<-runif(50,2,5)
> x3
[1] 3.619601 4.015782 4.927389 3.871766 2.559403 3.654698 3.636688 2.435611
[9] 3.919934 3.385902 2.155832 4.343270 2.306058 4.331264 3.824110 3.421138
[17] 3.014716 4.809355 3.545865 4.109747 4.496385 4.476492 4.824152 3.999915
[25] 2.369429 4.419645 3.556565 3.421748 3.185124 3.239173 4.180487 2.653179
[33] 4.674133 3.477992 3.933203 3.618354 2.064820 2.032384 3.086342 4.491011
[41] 4.361386 2.789445 3.881738 2.521680 3.185342 2.708259 2.023868 4.963704
[49] 4.574970 3.443716
> tail(sort(x3),10)
[1] 4.419645 4.476492 4.491011 4.496385 4.574970 4.674133 4.809355 4.824152
[9] 4.927389 4.963704
> x4<-rexp(50,0.75)
> x4
[1] 1.66408581 0.23668114 2.60394558 0.37745569 1.51734607 0.68286297
[7] 2.37845758 2.34748084 1.08916016 0.87455649 0.22715427 0.08631177
[13] 1.38793359 0.63791999 0.08081514 0.46960890 0.76566002 0.07207330
[19] 0.73923112 2.79757298 2.41873012 0.21448042 0.60012030 1.98638409
[25] 0.08985795 1.61284962 2.04608139 0.28587935 0.23873098 3.84622620
[31] 1.04341525 1.27033301 0.75144631 0.27834051 0.35531788 1.85149528
[37] 0.57331483 0.28346725 0.01938860 1.44158534 0.42863950 0.19755680
[43] 0.90512264 0.32139020 2.93323666 4.36947212 1.22103199 0.33063906
[49] 1.15281344 0.19477133
> tail(sort(x4),10)
[1] 1.986384 2.046081 2.347481 2.378458 2.418730 2.603946 2.797573 2.933237
[9] 3.846226 4.369472
> x5<-sample(1:100,50)
> x5
[1] 68 95 78 46 7 19 35 34 11 38 86 45 61 63 77 91 79 92 44
[20] 24 43 23 8 22 70 97 84 88 37 62 51 2 98 72 16 39 80 67
[39] 20 28 96 56 57 65 40 18 5 76 87 100
> tail(sort(x5),10)
[1] 86 87 88 91 92 95 96 97 98 100
> x6<-sample(500:1000,50)
> x6
[1] 699 622 523 634 547 986 929 774 612 725 607 752 686 796 891 859 553 810 720
[20] 900 712 745 769 604 626 990 511 874 609 942 723 509 747 549 534 679 751 896
[39] 881 892 706 694 613 775 606 705 521 637 651 709
> tail(sort(x6),10)
[1] 874 881 891 892 896 900 929 942 986 990
> x7<-sample(1:5,50,replace=TRUE)
> x7
[1] 1 5 5 5 3 4 5 3 2 5 2 4 3 4 3 2 5 5 5 3 2 3 1 2 1 3 4 3 3 2 5 4 5 5 1 5 3 1
[39] 3 4 3 4 3 1 5 5 3 3 2 1
> tail(sort(x7),10)
[1] 5 5 5 5 5 5 5 5 5 5
> x8<-rlnorm(50,meanlog=0,sdlog=1)
> x8
[1] 3.28565379 0.33035002 4.83159451 0.37365934 2.06412742 0.39767931
[7] 0.84730808 0.48102938 0.51692884 0.18420942 0.15586761 4.01837070
[13] 1.00295917 0.40872245 0.09060854 0.55788680 3.02236904 0.48929409
[19] 0.39801790 0.22944458 1.36532857 4.56598722 2.85700417 0.58897804
[25] 1.54894520 0.75960321 3.48848189 2.62556191 1.94963069 1.69519791
[31] 1.24759682 0.63056343 1.20232731 0.44990949 1.55569080 0.26933108
[37] 1.04039486 0.97568753 3.16183492 2.31072475 5.33267314 0.28704556
[43] 0.82927315 1.68154657 3.29524392 4.05284285 0.44683256 0.33032771
[49] 0.23528317 1.56624060
> tail(sort(x8),10)
[1] 3.022369 3.161835 3.285654 3.295244 3.488482 4.018371 4.052843 4.565987
[9] 4.831595 5.332673

更新于: 2020 年 09 月 04 日

189 次浏览

启动您的 职业

完成课程以获得认证

立即开始
广告