0

Suppose I've this following table:

     VAR    ITER_1  ITER_2  ITER_3  ITER_4
    VAR1    6       8       5       7
    VAR2    5       1       7       8
    VAR3    3       8       8       4
    VAR4    8       7       2       5
    VAR5    8       7       9       2
    VAR6    8       7       3       6
    VAR7    4       7       4       5

I want to select combinations of Columns for each Row such that there are combinations equal to a specific sum. E.g. in this case, suppose I want for each VAR combination of ITER to be 15. In that case I want to select for VAR1, ITER_2 & ITER_4. For VAR2, ITER_3 & ITER_4.

I wanted to develop an code such that code can tell me which column values to select for each VAR.

Can anybody plz suggest some method? One don't need to write the code, but the logic I can use.

Thank you.

Fernando
  • 7,785
  • 6
  • 49
  • 81
Beta
  • 1,638
  • 5
  • 33
  • 67
  • What should happen, if the desired sum is not possible? For VAR1, 16 would not be feasible. Which ITER should be taken, if multiple ITERs have the same value (eg ITER_1 and ITER_3 for VAR7)? – Axel Kemper Aug 07 '13 at 16:29
  • When I am stuck trying to figure out the logic for a problem, I sit down with pencil and paper and try some examples, and I observe the steps I follow when working through the examples. These steps usually lead to ideas on the logic necessary for solving the problem. Maybe this method will work for you. – SaganRitual Aug 07 '13 at 16:34
  • @Alex: Thanks for your comment. If multiple Iter has the same value then the code will highlight all of them. E.g. if there r 2 combinations for VAR1 that have 1 common iter (say ITER_3), then it will highlight 3 iters. But that possibility is negligible. Also, the dired sum is possible if we can put a range. But these r bit sofistications. If I get the solve the simple problem then we can add more complications later. – Beta Aug 07 '13 at 16:38
  • Thanks GreatBigBore! I'll try it out. – Beta Aug 07 '13 at 16:39

2 Answers2

2

This works if the sum is taken for all columns:

data = data.frame(x = 1:3, y = 2:4, z = 5:7)
sums = apply(data, 1, sum)
target.val = 11
which(sums == target.val)

Otherwise this looks like a exact cover problem. http://en.wikipedia.org/wiki/Exact_cover

Or

You could use a stochastic approach, like a Genetic Algorithm. A simplistic solution:

find.colsums = function(data, target,  N.tries = 100)
{
  nrows = nrow(data)
  max.cols = ncol(data)
  n.columns = sample(max.cols, N.tries, replace = TRUE)

  for (i in 1:N.tries){
    test.cols = sample(max.cols, n.columns[i])

    for (row in 1:nrows){
      if (sum(data[row, test.cols]) == target){
        cat("match at row:", row, "cols:", test.cols, "\n")
      }
    }
  }
}

Example:

data = data.frame(x = 1:3, y = 2:4, z = 5:7)
target = 7
find.colsums(data, target)

Fun with a Big dataset:

N = 1000
min.val = 1
max.val = 30
ncols = 10
target = ((min.val + max.val) * ncols/2)

data = matrix(sample(min.val:max.val, N, replace = TRUE), ncol = ncols)
find.colsums(data, target, N.tries = 1000)
Fernando
  • 7,785
  • 6
  • 49
  • 81
  • Thanks again! I just got involved in other works, so could not visit the site earlier. – Beta Aug 11 '13 at 05:31
0

You should look into recursive algorithms

You can find a good example here

Community
  • 1
  • 1
bastos.sergio
  • 6,684
  • 4
  • 26
  • 36