【问题标题】:Calculating number combinations through R通过 R 计算数字组合
【发布时间】:2020-12-02 09:16:12
【问题描述】:

我有以下数据。我想首先找出每个位置值上出现次数最多的数字。显然,一个地方可以有 0 到 9 的 10 种可能性。比我想要一个选项,我可以选择 5 个最高出现次数或 6 个或 7 或 8 个最高出现次数,例如如果我选择 5,那么程序应该取前 5 次出现,或者如果我选择 8,那么程序应该去掉最少出现的 2 个数字并取所有其他数字。

数据示例:

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

我想要选择 5、6、7 或 8 个最高出现次数和 2 或 3 或 4 个数字组合的选项

预期结果,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

预期结果,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

我尝试过的代码

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 in asplit(tmp, 2) : dim(x) must have a positive length 

输出结果:

> 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))

【问题讨论】:

  • 您想在每个位置都获得前 8 次出现?您能否以我们可以复制的可复制格式提供您的数据示例?
  • @RonakShah - 嗨,我已经编辑了数据格式,你可以复制。如果我想选择前 5、6、7 或 8 次出现,我想要一个选项,并且基于选择代码应该产生 2 位、3 位、4 位的组合结果。所以对于 8 次出现的选择,2 位组合将是 64 (8*8),3 位组合将是 512 (8*8*8),4 位组合将是 4096 个数字 (8*8*8*8)。跨度>
  • 对于 2 位组合,我们只考虑数据的前 2 位?对于 3 位数字,只考虑前 3 位数字等等?最高出现次数是基于每个单独的位置?
  • 是的,这是正确的,数据会告诉我们每个位置值的最高出现次数。

标签: r combinations


【解决方案1】:

我们可以写一个函数:

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

数据

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")

【讨论】:

  • 那是x 一个向量。如果您有数据作为名为 df 的数据框,请使用 df$column_name 而不是 x。类似generate_combinations(df$column_name, 2, 8)
  • 我在 excel 文件中只有这些数字的列表。
  • 您想在 excel 或 R 中执行此操作吗?如果您想在 R 中执行此操作,请将数据读入 R 并参考我上面的评论。
  • 是的 R 绝对是,所以我会读取 excel 文件并将其列分配给 x?你能提供这样做的代码吗?假设我的文件名是 numbers.xlsx
猜你喜欢
  • 2017-01-19
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 2021-08-16
相关资源
最近更新 更多