r - Find the largest product in a dataframe -
i have dataframe , need find largest product in data frame in same direction(up, down, left, right, or diagonally).
the dataframe follows:
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))
the dataframe looks this:
> 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
any suggestion highly appreciated.
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, , 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
or, more generally:
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 2 5 matrix m <- matrix(1:10, ncol=5) m[maxprodelements(m)] # [1] 2 4 6 8 10 prod(m[maxprodelements(m)]) # [1] 3840
Comments
Post a Comment