-3

データフレームがあり、データフレーム内で同じ方向 (上、下、左、右、または斜め) で最大の製品を見つける必要があります。

データフレームは次のとおりです。

structure(list(V1 = c(8L, 49L, 81L, 52L, 22L, 24L, 32L, 67L, 
24L, 21L, 78L, 16L, 86L, 19L, 4L, 88L, 4L, 20L, 20L, 1L), V2 = c(2L, 
49L, 49L, 70L, 31L, 47L, 98L, 26L, 55L, 36L, 17L, 39L, 56L, 80L, 
52L, 36L, 42L, 69L, 73L, 70L), V3 = c(22L, 99L, 31L, 95L, 16L, 
32L, 81L, 20L, 58L, 23L, 53L, 5L, 0L, 81L, 8L, 68L, 16L, 36L, 
35L, 54L), V4 = c(97L, 40L, 73L, 23L, 71L, 60L, 28L, 68L, 5L, 
9L, 28L, 42L, 48L, 68L, 83L, 87L, 73L, 41L, 29L, 71L), V5 = c(38L, 
17L, 55L, 4L, 51L, 99L, 64L, 2L, 66L, 75L, 22L, 96L, 35L, 5L, 
97L, 57L, 38L, 72L, 78L, 83L), V6 = c(15L, 81L, 79L, 60L, 67L, 
3L, 23L, 62L, 73L, 0L, 75L, 35L, 71L, 94L, 35L, 62L, 25L, 30L, 
31L, 51L), V7 = c(0L, 18L, 14L, 11L, 63L, 45L, 67L, 12L, 99L, 
76L, 31L, 31L, 89L, 47L, 99L, 20L, 39L, 23L, 90L, 54L), V8 = c(40L, 
57L, 29L, 42L, 89L, 2L, 10L, 20L, 26L, 44L, 67L, 47L, 7L, 69L, 
16L, 72L, 11L, 88L, 1L, 69L), V9 = c(0L, 60L, 93L, 69L, 41L, 
44L, 26L, 95L, 97L, 20L, 15L, 55L, 5L, 28L, 7L, 3L, 24L, 34L, 
74L, 16L), V10 = c(75L, 87L, 71L, 24L, 92L, 75L, 38L, 63L, 17L, 
45L, 94L, 58L, 44L, 73L, 97L, 46L, 94L, 62L, 31L, 92L), V11 = c(4L, 
17L, 40L, 68L, 36L, 33L, 40L, 94L, 78L, 35L, 3L, 88L, 44L, 92L, 
57L, 33L, 72L, 99L, 49L, 33L), V12 = c(5L, 40L, 67L, 56L, 54L, 
53L, 67L, 39L, 78L, 14L, 80L, 24L, 37L, 13L, 32L, 67L, 18L, 69L, 
71L, 48L), V13 = c(7L, 98L, 53L, 1L, 22L, 78L, 59L, 63L, 96L, 
0L, 4L, 0L, 44L, 86L, 16L, 46L, 8L, 82L, 48L, 61L), V14 = c(78L, 
43L, 88L, 32L, 40L, 36L, 54L, 8L, 83L, 61L, 62L, 17L, 60L, 52L, 
26L, 55L, 46L, 67L, 86L, 43L), V15 = c(52L, 69L, 30L, 56L, 40L, 
84L, 70L, 40L, 14L, 33L, 16L, 54L, 21L, 17L, 26L, 12L, 29L, 59L, 
81L, 52L), V16 = c(12L, 48L, 3L, 71L, 28L, 20L, 66L, 91L, 88L, 
97L, 14L, 24L, 58L, 77L, 79L, 32L, 32L, 85L, 16L, 1L), V17 = c(50L, 
4L, 49L, 37L, 66L, 35L, 18L, 66L, 34L, 34L, 9L, 36L, 51L, 4L, 
33L, 63L, 40L, 74L, 23L, 89L), V18 = c(77L, 56L, 13L, 2L, 33L, 
17L, 38L, 49L, 89L, 31L, 53L, 29L, 54L, 89L, 27L, 93L, 62L, 4L, 
57L, 19L), V19 = c(91L, 62L, 36L, 36L, 13L, 12L, 64L, 94L, 63L, 
33L, 56L, 85L, 17L, 55L, 98L, 53L, 76L, 36L, 5L, 67L), V20 = c(8L, 
0L, 65L, 91L, 80L, 50L, 70L, 21L, 72L, 95L, 92L, 57L, 58L, 40L, 
66L, 69L, 36L, 16L, 54L, 48L)), .Names = c("V1", "V2", "V3", 
"V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", 
"V14", "V15", "V16", "V17", "V18", "V19", "V20"), class = "data.frame", row.names = c(NA, 
-20L))

データフレームは次のようになります。

> newjd
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
1   8  2 22 97 38 15  0 40  0  75   4   5   7  78  52  12  50  77  91   8
2  49 49 99 40 17 81 18 57 60  87  17  40  98  43  69  48   4  56  62   0
3  81 49 31 73 55 79 14 29 93  71  40  67  53  88  30   3  49  13  36  65
4  52 70 95 23  4 60 11 42 69  24  68  56   1  32  56  71  37   2  36  91
5  22 31 16 71 51 67 63 89 41  92  36  54  22  40  40  28  66  33  13  80
6  24 47 32 60 99  3 45  2 44  75  33  53  78  36  84  20  35  17  12  50
7  32 98 81 28 64 23 67 10 26  38  40  67  59  54  70  66  18  38  64  70
8  67 26 20 68  2 62 12 20 95  63  94  39  63   8  40  91  66  49  94  21
9  24 55 58  5 66 73 99 26 97  17  78  78  96  83  14  88  34  89  63  72
10 21 36 23  9 75  0 76 44 20  45  35  14   0  61  33  97  34  31  33  95
11 78 17 53 28 22 75 31 67 15  94   3  80   4  62  16  14   9  53  56  92
12 16 39  5 42 96 35 31 47 55  58  88  24   0  17  54  24  36  29  85  57
13 86 56  0 48 35 71 89  7  5  44  44  37  44  60  21  58  51  54  17  58
14 19 80 81 68  5 94 47 69 28  73  92  13  86  52  17  77   4  89  55  40
15  4 52  8 83 97 35 99 16  7  97  57  32  16  26  26  79  33  27  98  66
16 88 36 68 87 57 62 20 72  3  46  33  67  46  55  12  32  63  93  53  69
17  4 42 16 73 38 25 39 11 24  94  72  18   8  46  29  32  40  62  76  36
18 20 69 36 41 72 30 23 88 34  62  99  69  82  67  59  85  74   4  36  16
19 20 73 35 29 78 31 90  1 74  31  49  71  48  86  81  16  23  57   5  54
20  1 70 54 71 83 51 54 69 16  92  33  48  61  43  52   1  89  19  67  48

どんな提案でも大歓迎です。

4

1 に答える 1

4
m <- as.matrix(df)
ll <- c(lapply(1:20, function(X) row(m)==X),                 ## Rows
        lapply(1:20, function(X) col(m)==X),                 ## Columns
        lapply(-19:19, function(X) (col(m)+X)==row(m)),      ## NW-SE diagonals
        lapply(-19:19, function(X) (col(m)+X)==(21-row(m)))) ## SW-NE diagonals
## Calculate product of each row, column, and diagonal
pp <- sapply(ll, function(X) prod(m[X]))
max(pp)
# [1] 1.824798e+35
m[ll[[which.max(pp)]]]
# [1] 75 87 71 24 92 75 38 63 17 45 94 58 44 73 97 46 94 62 31 92

または、より一般的に:

maxProdElements <- function(m) {
    mm <- nrow(m)
    nn <- ncol(m)
    ll <- c(lapply(seq_len(mm), function(X) which(row(m)==X)),
            lapply(seq_len(nn), function(X) which(col(m)==X)),
            lapply((-mm+1):(nn-1), function(X) which(col(m)==(row(m)+X))),
            lapply((-mm+1):(nn-1), function(X) which((nn+1-col(m))==(row(m)+X))))
    pp <- sapply(ll, function(X) prod(m[X]))
    ll[[which.max(pp)]]
}

## Try it with a 2 by 5 matrix
M <- matrix(1:10, ncol=5)
M[maxProdElements(M)]
# [1]  2  4  6  8 10
prod(M[maxProdElements(M)])
# [1] 3840
于 2013-04-19T23:20:52.877 に答える