0

I have a dataframe

df= data.frame(ID=paste(rep("a",7),c(1:7),sep = ""),
               col2=c(12,10,1,2,5,10,8),
               col3=c(200,150,180,450,100,130,200))

I'm trying to check a combination of those element that sum upto a particular number AND for those same elements checking a sumproduct But it is not giving any output For e.g from dataframe df these values make a sum of 25, so

  col2  col3
    10   150
    5    100
    10   130

=sumproduct((E3:E5,D3:D5) [excel formula] 
output = 3300

And this is I expect as output from following code a2,a5,a6

This is my code

for(i in 1:nrow(df)){
  comb <- combn(1:nrow(df), i, FUN = NULL, simplify = TRUE)
  for (j in 1:ncol(comb)){
    subvec <- comb[,j]
    a <- sum(df[subvec,2])

     b <- sum(df[i, 2] * df[i, 3])
    if(a == 25 && b == 3300){
      print(df[subvec,1])
    }
  }
}
Andre_k
  • 1,680
  • 3
  • 18
  • 41

0 Answers0