0

I'm relatively new to R and try to find a faster way for my problem. I want to do some constraint programming and I have lists, that contains all possible values for each variable. In order to find a feasible solution, I have to do some cuts, which I realize with a lot of loops at the moment. I hope there is a way to do something like "lapply", but I also need to know the related value of another list and don't know, how to do this.

Here's an example:

y=rep(list(rep(list(c(0, 1)), N)), S);

job=-1:J
job=rep(list(rep(list(rep(list(job), J)), N)), S)

for (s in 1:S){
  for (n in 1:N){
    if (length(y[[s]][[n]])==1 && y[[s]][[n]]==0){
      job[[s]][[n]]=rep(list(-1), J);
    }
  }
}

There are more of these for-loops following and I know that there will be no changes at the beginnig, because y[[s]][[n]] has length 2, but this will be changed during the next steps.

I hope you understand my problem and thank you for answering!

R. Akina
  • 3
  • 2
  • Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 May 02 '16 at 09:18

1 Answers1

0

There is a way to iterate two lists with lapply (they have to have the same length) simultaneously, just like that:

x1 = list(1, 2, 3)
y1 = list(3, 2, 3)

lapply(seq(1, length(x1)), function(i, x, y){
  if(x[[i]] == y[[i]]){
    return(TRUE)
  } else{
    return(FALSE)
  }
}, x = x1, y = y1)

of course, you can nest that solution to check another lists in an element of a current list item.

kodi1911
  • 662
  • 1
  • 18
  • 34