0
votes

I am currently experiencing somme difficulties in a R script, since the conditions required increase within the loop. Here's the explanation and an example.

  1. Imagine a 10x10 matrix with 1 or 0 (either LISTE or Meldungen)

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 0 0 0 1 0 0 1 0 1 [2,] 1 0 0 0 1 0 1 0 0 1 [3,] 0 0 0 1 0 0 0 0 1 0 [4,] 1 0 1 0 0 0 0 0 1 1 [5,] 1 0 1 0 1 1 0 0 1 0 [6,] 0 1 1 1 0 1 1 1 0 1 [7,] 0 0 1 0 1 1 1 1 0 0 [8,] 1 1 1 0 0 0 1 1 1 0 [9,] 1 0 0 0 0 1 1 0 0 0 [10,] 1 1 1 0 1 0 1 0 1 0 I would like a loop that loads this database at every iteration. And check the foloowing possibilities for every row and colum(i row and j column)

1st iteration 1. starting at j=1 to number of column if i,1=1 and i+1,1=0 then i+2,1=0

2 iteration (with original database) starting at j=1 to number of column if i,1=1 and i+1,1=0 and i+2,1=0 then i+3,1=0

3 iteration (with original database) if i,1=1 and i+1,1=0 and i+2,1=0 and i+3,1=0 then i+4,1=0

and so on

I tried to following code

for(p in 2:time){
  LISTE_neu <- LISTE        #load original database      

  if(p == 2){
    for(i in 1:row_number){
      for(j in (1+p):column_number)){
        if(LISTE_neu[i,(j-p)]==1 & Meldungen_num[i,j-(p-1)]==0){LISTE_neu[i,j] <- 0}
      }
    }
    LISTE_neu_2 <- LISTE_neu
  }

  if(p == 3){
    for(i in 1:row_number){
      for(j in (1+p):column_number){
        if(LISTE_neu[i,(j-p)]==1 & Meldungen_num[i,j-(p-1)]==0 & Meldungen_num[i,(j-(p-2))]==0){LISTE_neu[i,j] <- 0}
      }
    }
    LISTE_neu_3 <- LISTE_neu
  }
}

However at this point it's working. However it's very painful to type this increasing conditions.

Do you have an idea how to make things simpler? le tme know if it's unclear

1
Please explain your actual goal in words and provide example input and expected output. Nested for loops is not how we do such things in R. - Roland
Sure. In this case, I have to update the code written by someone else. However the goal is that at each iteration the number of conditions in the if function increases and it's directly linked with p. I was wondering if it's possible to increate them into the loop in order to avoid repetitions - elio rico
Well, this depends entirely on the conditions that you will create. Would the next extra condition be Meldungen_num[i,j-(p-3)]==0? Then yes, you can probably do this very easily. - slamballais
Again, you are explaining a step not the ultimate goal. And we need input and expected output to test solutions. - Roland
Also, in your if(p==3) it says i in 1:401... is this supposed to be i in 1:length_GP? And is j in (1+p):10 the same as j in (1+p):length(quartale)? Because then you can save a lot of code. - slamballais

1 Answers

0
votes

Edit: Just for clarification... The problem currently states that in a column, if the first entry is 1 and all subsequent entries up until p are 0, then p+1 should be 0. Each p should yield its own data frame, so we get p data frames.

Edit2: Updated according to clearer wishes.

lis <- list()

for (p in 2:9){
  LISTE_neu <- LISTE
  met <- LISTE_neu[1,] == 1 & colSums(Meldungen_num[2:p,]) == 0
  LISTE_neu[p+1,met] <- 0
  lis[[p]] <- LISTE_neu
}

Call the results with lis[[p]], so for p = 2 you use lis[[2]].

Explanation: Basically, as p increases, you want to look at a bigger part of Meldungen_num and see if all values are 0. Since all values must be 0, their sum must also be 0. Therefore, I take all the values of interest (Meldungen_num[2:p,]) and sum them (colSums). Note that this happens for all columns simultaneously (so we get as many sums as there are columns). I then compare all these to 0, and for each column separately you get the answer. So that one single line checks all the columns for a given p all at once. met then contains which columns satisfy your conditions.

Then, I write 0 to only those columns that met our conditions using LISTE_neu[p+1,met] <- 0. Then, I store this new table in the list lis and go to the next p.