title: Using apply on rows of a dataframe with missing data & varying function by row - r
body: <p>Here is a sample dataframe called newdf. AA, AB, AC etc. are rownames:</p>
  
  <pre><code>      value    c1    c2    c3    c4
  AA    0.875 0.750 0.750 0.625 1.000
  AB    1.000 1.000 0.812 0.562 0.375
  AC    0.625 0.812 0.667 0.812 0.750
  AD    0.375 0.625 0.938    NA 0.875
  AE       NA 0.500 0.542 0.938 0.500
  BA    1.000 0.958 0.875 0.875 1.000
  BB    0.875 0.938 0.812 1.000 0.562
  BC    0.750 0.708 0.750    NA 0.500
  BD    0.500 0.333 0.750    NA 0.625
  BE       NA 0.208 0.500    NA 0.250
  CA       NA 0.938 1.000 0.562    NA
  CB       NA    NA    NA 0.938 0.812
  CC    1.000 1.000 0.750 1.000 1.000
  CD    0.938 0.812    NA    NA    NA
  CE       NA 0.688 0.875    NA 0.938
  DA       NA    NA 0.625 1.000    NA
  DB       NA    NA 1.000 0.375 0.625
  DC       NA    NA 0.750 0.625 1.000
  DD       NA    NA    NA    NA    NA
  DE       NA 1.000 0.500 0.750 0.750
  EA    1.000    NA    NA 0.375    NA
  EB    0.875    NA    NA 0.750 1.000
  EC    0.250    NA 1.000 1.000    NA
  ED    0.000    NA    NA    NA    NA
  EE       NA    NA    NA    NA    NA
  </code></pre>
  
  <p>I wish to use apply over rows.   I know how to do this quite simply, if my function argument is consistent. </p>
  
  <p>For example, I wish to know how many times c1,c2,c3,c4 are >0.9, I would do this by row:</p>
  
  <pre><code>z&lt;-newdf[-1]
  apply(z, 1, function(x)  sum(x &gt;= 0.9, na.rm=T)) #how many columns are &gt;= 0.9
  
  #AA AB AC AD AE BA BB BC BD BE CA CB CC CD CE DA DB DC DD DE EA EB EC ED EE 
  # 1  1  0  1  1  2  2  0  0  0  2  1  3  0  1  1  1  1  0  1  0  1  2  0  0 
  </code></pre>
  
  <p>However, what I really want to know is how many columns from c1 onwards are greater than the value stored in the first variable/column of newdf.  I tried this, which returns garbage:</p>
  
  <pre><code>value&lt;-newdf[,1]
  apply(z, 1, function(x)  sum(x &gt;= value, na.rm=T)) #how many columns are &gt;= value
  </code></pre>
  
  <p>I tried passing extra arguments to the function similar to the apply help pages but am not getting it right.</p>
  
  <h3>#</h3>
  
  <p>example data:</p>
  
  <pre><code> newdf&lt;-structure(list(value = structure(c(0.875, 1, 0.625, 0.375, NA, 
   1, 0.875, 0.75, 0.5, NA, NA, NA, 1, 0.938, NA, NA, NA, NA, NA, 
   NA, 1, 0.875, 0.25, 0, NA), .Dim = 25L, .Dimnames = list(c("AA", 
   "AB", "AC", "AD", NA, "BA", "BB", "BC", "BD", NA, NA, NA, "CC", 
   "CD", NA, NA, NA, NA, NA, NA, "EA", "EB", "EC", "ED", NA))), 
       c1 = structure(c(0.75, 1, 0.812, 0.625, 0.5, 0.958, 0.938, 
       0.708, 0.333, 0.208, 0.938, NA, 1, 0.812, 0.688, NA, NA, 
       NA, NA, 1, NA, NA, NA, NA, NA), .Dim = 25L, .Dimnames = list(
           c("AA", "AB", "AC", "AD", "AE", "BA", "BB", "BC", "BD", 
           "BE", "CA", NA, "CC", "CD", "CE", NA, NA, NA, NA, "DE", 
           NA, NA, NA, NA, NA))), c2 = structure(c(0.75, 0.812, 
       0.667, 0.938, 0.542, 0.875, 0.812, 0.75, 0.75, 0.5, 1, NA, 
       0.75, NA, 0.875, 0.625, 1, 0.75, NA, 0.5, NA, NA, 1, NA, 
       NA), .Dim = 25L, .Dimnames = list(c("AA", "AB", "AC", "AD", 
       "AE", "BA", "BB", "BC", "BD", "BE", "CA", NA, "CC", NA, "CE", 
       "DA", "DB", "DC", NA, "DE", NA, NA, "EC", NA, NA))), c3 = structure(c(0.625, 
       0.562, 0.812, NA, 0.938, 0.875, 1, NA, NA, NA, 0.562, 0.938, 
       1, NA, NA, 1, 0.375, 0.625, NA, 0.75, 0.375, 0.75, 1, NA, 
  NA), .Dim = 25L, .Dimnames = list(c("AA", "AB", "AC", NA, 
  "AE", "BA", "BB", NA, NA, NA, "CA", "CB", "CC", NA, NA, "DA", 
  "DB", "DC", NA, "DE", "EA", "EB", "EC", NA, NA))), c4 = structure(c(1, 
  0.375, 0.75, 0.875, 0.5, 1, 0.562, 0.5, 0.625, 0.25, NA, 
  0.812, 1, NA, 0.938, NA, 0.625, 1, NA, 0.75, NA, 1, NA, NA, 
  NA), .Dim = 25L, .Dimnames = list(c("AA", "AB", "AC", "AD", 
  "AE", "BA", "BB", "BC", "BD", "BE", NA, "CB", "CC", NA, "CE", 
  NA, "DB", "DC", NA, "DE", NA, "EB", NA, NA, NA)))), .Names = c("value", 
  "c1", "c2", "c3", "c4"), class = "data.frame", row.names = c("AA", 
  "AB", "AC", "AD", "AE", "BA", "BB", "BC", "BD", "BE", "CA", "CB", 
  "CC", "CD", "CE", "DA", "DB", "DC", "DD", "DE", "EA", "EB", "EC", 
  "ED", "EE"))
  </code></pre>