Search code examples
rfor-loopdataframeinfinite-loop

For Loop Adding Extra Rows to The Data Frame


Hello I am very new to the programming world and data science as well, and I am trying to work my way through it.

I am trying to assign values to the column in a data frame and using for loop such that the data frame is divided into ten groups and every row in every group is assigned a rank, such that row 1 to 10 is assigned as rank 1 and row 11 to 20 is assigned as rank 2 and so on. The original dimension of subset data set is 100 * 6

My data frame looks like Data Frame

The codes I have written are:

   x <- round(nrow(subset) / 10)
   a=1
   for(j in 1:10){
    for(i in a:x){
      subset[i, "rank"] = j
    }
    j = j + 1
    a = x + 1
    x = x * j
    }

However, the loop runs infinitely and keeps on adding additional rows to the data frame. I had to manually stop the loop and the resulting dimension of the subset data frame was 17926 * 6.

Please help me understand where am I going wrong in writing the loop.

P.S. subset is a data frame name and not the subset function in R

Thanks in Advance !!


Solution

  • It might be better for you to start working with vectorized calculations instead of loops. This will help you in the future.

    For example:

    df <- data.frame(x = 1:100)
    df$rank <- (df$x-1)%/%10 + 1
    df  
    

    results in:

        x rank
    1   1    1
    2   2    1
    3   3    1
    4   4    1
    5   5    1
    6   6    1
    7   7    1
    8   8    1
    9   9    1
    10 10    1
    11 11    2
    12 12    2
    13 13    2
    14 14    2
    15 15    2
    16 16    2
    17 17    2
    18 18    2
    19 19    2
    20 20    2
    21 21    3
    22 22    3
    23 23    3
    24 24    3
    25 25    3