mercredi 20 mai 2015

R - infinite loop if

I am trying to remove useless column from a data frame. I used a while loop with an if statement, and it seems that it never leaves the if statement :

it = 1
while (it < ncol(testing))
{
  if ("drop" %in% CategOfData[it,])
   {testing[,it]<-NULL}
  else it = it + 1
}

the if loop works as long as it's not nested in a while loop.

testing is my data frame containing 400 rows and 12 columns,

CategOfData is a data frame of 12 rows and 2 columns, CategOfData contains the header of my df "testing" and the categories of it, 3 rows contain the word "drop"

I tested this code by replacing {testing[,it]<-NULL} with {jkl = jkl + 0.5}, And again the code ran long, I cut it short, asked the console what the value of jkl was, and it returned a number well over 800 000, while it should have returned 2.5 (1 + 3*0.5)

I don't understand why it nevers enters the else part of the code. which makes the while loop infinite since "it" never incrementes

I would use a for loop, but R doesn't agree since I'm dropping columns as I go.

the type of CategOfData :

   > CategOfData [1,]
              header              x 
         "PIERRE.MARIE"         "drop" 

and "testing"

 > head(testing[,1])
 [1] PIERRE-MARIE PIERRE-MARIE PIERRE-MARIE PIERRE-MARIE PIERRE-MARIE  PIERRE-MARIE
       Levels: LAURENNE PIERRE-MARIE

Can you help me pinpoint where the problem lies please?

Aucun commentaire:

Enregistrer un commentaire