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.
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 0I 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
forloops is not how we do such things in R. - RolandMeldungen_num[i,j-(p-3)]==0? Then yes, you can probably do this very easily. - slamballaisif(p==3)it saysi in 1:401... is this supposed to bei in 1:length_GP? And isj in (1+p):10the same asj in (1+p):length(quartale)? Because then you can save a lot of code. - slamballais