简体   繁体   English

通过 R 计算数字组合

[英]Calculating number combinations through R

I have following data.我有以下数据。 i want to first find out the most occurring digit on every place value.我想首先找出每个位值上出现次数最多的数字。 Obviously one place can have 10 possibilities from 0 to 9. Than i want an option where by i can choose 5 top occurrences or 6 or 7 or 8 top occurrences for eg if i choose 5 then the program should take the top 5 occurrences or if i choose 8 then program should leave out the least 2 occurring digits and take all others.显然,一个地方可以有 10 种可能性,从 0 到 9。比我想要一个选项,我可以选择 5 个最高出现次数或 6、7 或 8 个最高出现次数,例如,如果我选择 5,那么程序应该选择前 5 个出现次数,或者如果我选择 8 然后程序应该去掉最少出现的 2 个数字并取所有其他数字。

Data example:数据示例:

076060
693022
585821
980575
438068
766214
051726
060417
822591
015507
635576
180231
212238
417651
631269
720767
348344
532148
748085
474026
380897
512421
749492
423616
950330
930079
097759
638901
319356
683308
818127
880675
256095
639187
339904
945437
799571
466063
428853
397799
782034
462486
739342
879023
419264
793319
603131
315791
351701
151747
365656
982700
348093
793392
946875
912108
070001
780515
222468
345439
234846
227112
757243
341747
480781
906624
868265
388572
947873
898895
452518
738580
217342
849951
437382
247068
743776
562584
636948
049434
139296
688436
443629

I want option of choosing 5, 6,7 or 8 top occurrences and 2 or 3 or 4number combination我想要选择 5、6、7 或 8 个最高出现次数和 2 个或 3 个或 4 个数字组合的选项

Expected results, 2 number combination basis top 8 occurrences and so on.预期结果,2 个数字组合基于前 8 次出现等。

01
02
03
04
05
06
08
09
21
22
23
24
25
26
28
29
31
32
33
34
35
36
38
39
41
42
43
44
45
46
48
49
61
62
63
64
65
66
68
69
71
72
73
74
75
76
78
79
81
82
83
84
85
86
88
89
91
92
93
94
95
96
98
99

Expected results, 3 number combination basis top 8 occurrences and so on.预期结果,3 个数字组合基于前 8 次出现等。

010
012
013
015
016
017
018
019
020
022
023
025
026
027
028
029
030
032
033
035
036
037
038
039
040
042
043
045
046
047
048
049
050
052
053
055
056
057
058
059
060
062
063
065
066
067
068
069
080
082
083
085
086
087
088
089
090
092
093
095
096
097
098
099
210
212
213
215
216
217
218
219
220
222
223
225
226
227
228
229
230
232
233
235
236
237
238
239
240
242
243
245
246
247
248
249
250
252
253
255
256
257
258
259
260
262
263
265
266
267
268
269
280
282
283
285
286
287
288
289
290
292
293
295
296
297
298
299
310
312
313
315
316
317
318
319
320
322
323
325
326
327
328
329
330
332
333
335
336
337
338
339
340
342
343
345
346
347
348
349
350
352
353
355
356
357
358
359
360
362
363
365
366
367
368
369
380
382
383
385
386
387
388
389
390
392
393
395
396
397
398
399
410
412
413
415
416
417
418
419
420
422
423
425
426
427
428
429
430
432
433
435
436
437
438
439
440
442
443
445
446
447
448
449
450
452
453
455
456
457
458
459
460
462
463
465
466
467
468
469
480
482
483
485
486
487
488
489
490
492
493
495
496
497
498
499
610
612
613
615
616
617
618
619
620
622
623
625
626
627
628
629
630
632
633
635
636
637
638
639
640
642
643
645
646
647
648
649
650
652
653
655
656
657
658
659
660
662
663
665
666
667
668
669
680
682
683
685
686
687
688
689
690
692
693
695
696
697
698
699
710
712
713
715
716
717
718
719
720
722
723
725
726
727
728
729
730
732
733
735
736
737
738
739
740
742
743
745
746
747
748
749
750
752
753
755
756
757
758
759
760
762
763
765
766
767
768
769
780
782
783
785
786
787
788
789
790
792
793
795
796
797
798
799
810
812
813
815
816
817
818
819
820
822
823
825
826
827
828
829
830
832
833
835
836
837
838
839
840
842
843
845
846
847
848
849
850
852
853
855
856
857
858
859
860
862
863
865
866
867
868
869
880
882
883
885
886
887
888
889
890
892
893
895
896
897
898
899
910
912
913
915
916
917
918
919
920
922
923
925
926
927
928
929
930
932
933
935
936
937
938
939
940
942
943
945
946
947
948
949
950
952
953
955
956
957
958
959
960
962
963
965
966
967
968
969
980
982
983
985
986
987
988
989
990
992
993
995
996
997
998
999

code i have tried我试过的代码

getwd()
setwd("C:/Users/aziq/Desktop")
library(xlsx)
x <- read.xlsx("numbers.xlsx","Sheet1")

    generate_combinations <- function(x, pos, n) {
      #select first pos characters from each string
      #split each character and create a matrix
      mat <- do.call(rbind, strsplit(substr(x, 1, pos), ''))
      #Find top n occurrence in each column of matrix
      tmp <- apply(mat, 2, function(x) tail(names(sort(table(x))), n))
      #Create all combinations of top occurrences.
      do.call(expand.grid, asplit(tmp, 2))
    }
    
    generate_combinations(x, 2, 8)
    nrow(generate_combinations(x, 2, 8))

Error it is showing它显示的错误

Error in asplit(tmp, 2) : dim(x) must have a positive length 

Dput results:输出结果:

> dput(x)
structure(list(X076060 = c("693022", "585821", "980575", "438068", 
"766214", "051726", "060417", "822591", "015507", "635576", "180231", 
"212238", "417651", "631269", "720767", "348344", "532148", "748085", 
"474026", "380897", "512421", "749492", "423616", "950330", "930079", 
"097759", "638901", "319356", "683308", "818127", "880675", "256095", 
"639187", "339904", "945437", "799571", "466063", "428853", "397799", 
"782034", "462486", "739342", "879023", "419264", "793319", "603131", 
"315791", "351701", "151747", "365656", "982700", "348093", "793392", 
"946875", "912108", "070001", "780515", "222468", "345439", "234846", 
"227112", "757243", "341747", "480781", "906624", "868265", "388572", 
"947873", "898895", "452518", "738580", "217342", "849951", "437382", 
"247068", "743776", "562584", "636948", "049434", "139296", "688436", 
"443629")), class = "data.frame", row.names = c(NA, -82L))

We can write a function :我们可以写一个函数:

generate_combinations <- function(x, pos, n) {
  if(pos == 1) {
    return(data.frame(Var1 = names(sort(table(substr(x, 1, pos)),
                      = decreasing = TRUE)[1:n])))
  }
  #select first pos characters from each string
  #split each character and create a matrix
  mat <- do.call(rbind, strsplit(substr(x, 1, pos), ''))
  #Find top n occurrence in each column of matrix
  tmp <- apply(mat, 2, function(x) tail(names(sort(table(x))), n))
  #Create all combinations of top occurrences.
  do.call(expand.grid, asplit(tmp, 2))
}

generate_combinations(x, 2, 8)
#   Var1 Var2
#1     0    2
#2     2    2
#3     8    2
#4     6    2
#5     9    2
#6     3    2
#7     4    2
#8     7    2
#9     0    5
#10    2    5
#...
#...

nrow(generate_combinations(x, 2, 8))
#[1] 64
nrow(generate_combinations(x, 3, 8))
#[1] 512

data数据

x <- c("076060", "693022", "585821", "980575", "438068", "766214", 
"051726", "060417", "822591", "015507", "635576", "180231", "212238", 
"417651", "631269", "720767", "348344", "532148", "748085", "474026", 
"380897", "512421", "749492", "423616", "950330", "930079", "097759", 
"638901", "319356", "683308", "818127", "880675", "256095", "639187", 
"339904", "945437", "799571", "466063", "428853", "397799", "782034", 
"462486", "739342", "879023", "419264", "793319", "603131", "315791", 
"351701", "151747", "365656", "982700", "348093", "793392", "946875", 
"912108", "070001", "780515", "222468", "345439", "234846", "227112", 
"757243", "341747", "480781", "906624", "868265", "388572", "947873", 
"898895", "452518", "738580", "217342", "849951", "437382", "247068", 
"743776", "562584", "636948", "049434", "139296", "688436", "443629")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM