Search code examples
rrecode

Conditional recoding in R


Here is an excerpt from an R data.frame which consist of indicators. It can be seen that for some rows the indictors switch to 1 until the end of the row while others switch back to zero.

       [,18] [,19] [,20] [,21] [,22] [,23] [,24] 
   [1,]     0     0     0     0     0     0     0 
   [2,]     0     0     0     0     1     1     1  
   [6,]     0     0     0     0     0     0     0  
   [7,]     0     0     1     0     0     1     1  
   [8,]     0     0     0     0     0     0     0  
   [9,]     0     1     0     1     1     1     1  
  [10,]     1     1     1     1     1     1     1  
  [11,]     1     0     1     1     0     1     1

I want to identify the point in each row when a sequence of 1 does not change back to zero anymore to the end of the row. This should be indicated by all elements set to 1 after this point, while the previous elements are 0. The result would be:

       [,18] [,19] [,20] [,21] [,22] [,23] [,24] 
   [1,]     0     0     0     0     0     0     0 
   [2,]     0     0     0     0     1     1     1  
   [6,]     0     0     0     0     0     0     0  
   [7,]     0     0     0     0     0     1     1  
   [8,]     0     0     0     0     0     0     0  
   [9,]     0     0     0     1     1     1     1  
  [10,]     1     1     1     1     1     1     1  
  [11,]     0     0     0     0     0     1     1

Solution

  • You can take the cumulative min from the end of each row:

    t(apply(m, 1, function(r) rev(cummin(rev(r)))))
    
    #     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
    #[1,]    0    0    0    0    0    0    0
    #[2,]    0    0    0    0    1    1    1
    #[3,]    0    0    0    0    0    0    0
    #[4,]    0    0    0    0    0    1    1
    #[5,]    0    0    0    0    0    0    0
    #[6,]    0    0    0    1    1    1    1
    #[7,]    1    1    1    1    1    1    1
    #[8,]    0    0    0    0    0    1    1
    

    Data: m is an integer matrix consisting of 1 and 0

    dput(m)
    structure(c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 
    1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 
    1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 
    1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L), .Dim = c(8L, 7L))