Ramiro Guzmán
Ramiro Guzmán

Reputation: 45

for and if cicle operations

Hi¡ I have a doubt and I hope someone can help me please, I have a dataframe in R and it makes a double cicle for and an if, the data frame has some values and then if the condition is True, it makes some operations, the problem is I can't understand neither the cicle and the operation the code makes under the condition. I reply the code I have in a simpler one but the idea is the same. And if someone can explain me the whole operation please.

w<-c(2,5,4,3,5,6,8,2,4,6,8)
x<-c(2,5,6,7,1,1,4,9,8,8,2)
y<-c(2,5,6,3,2,4,5,6,7,3,5)
z<-c(2,5,4,5,6,3,2,5,6,4,6)

letras<-data.frame(w,x,y,z)

l=1
o=1
v=nrow(letras)
letras$op1<-c(1)
letras$op2<-c(0)
for (l in 1:v) {
  for (o in 1:v) {
    if(letras$x[o]==letras$y[l] & letras$z[l]==letras$z[o] & letras$w[l]){
      letras$op1<-letras$op1+1
      letras$op2<-letras$x*letras$y
    }
    
  }
  
}

The result is the following: enter image description here

Thanks¡¡¡¡¡

Upvotes: 0

Views: 59

Answers (1)

Colin Daglish
Colin Daglish

Reputation: 77

This segment of code is storing values into vectors labeled w,x,y,z.

w<-c(2,5,4,3,5,6,8,2,4,6,8)
x<-c(2,5,6,7,1,1,4,9,8,8,2)
y<-c(2,5,6,3,2,4,5,6,7,3,5)
z<-c(2,5,4,5,6,3,2,5,6,4,6)

It then transforms the 4 vectors into a data frame

letras<-data.frame(w,x,y,z)

This bit of code isn't doing anything as far as I can tell.

l=1 #???
o=1 #???

This counts how many rows is in the letras data frame and stores to v, in this case 11 rows.

v=nrow(letras) 

This creates new columns in letras dataframe with all ones in op1 and all zeros in op2

letras$op1<-c(1)
letras$op2<-c(0)

Here each for loop is acting as a counter, and will run the code beneath it iteratively from 1 to v (11), so 11 iterations. Each iteration the value of l will increase by 1. So first iteration l = 1, second l=2... etc.

for (l in 1:v) {

You then have a second counter, which is running within the first counter. So this will iterate over 1 to 11, exactly the same way as above. But the difference is, this counter will need to complete it's 1 to 11 cycle before the top level counter can move onto the next number. So o will effectively cycle from 1 to 11, for each 1 count of 1l. So with the two together, the inside for loop will count from 1 to 11, 11 times.

  for (o in 1:v) {

You then have a logical statement which will run the code beneath if the column x and column y values are the same. Remember they will be calling different index values so it could be 1st x value vs the 2nd y value. There is an AND statement so it also needs the two z position values to be equal. and the last part letras$w[l] is always true in this particular example, so could possibly be removed.

    if(letras$x[o]==letras$y[l] & letras$z[l]==letras$z[o] & letras$w[l]){

Lastly, is the bit that happens if the above statement is true. op1 get's 1 added (remember this was starting from 1 anyway), and op2 multiplies x*y columns together. This multiplication is perhaps a little bit inefficient, because x and y do not change, so the answer will calculate the same result each time the the if statement evaluates TRUE.

      letras$op1<-letras$op1+1
      letras$op2<-letras$x*letras$y
    }    
  }
}

Hope this helps.

Upvotes: 2

Related Questions