title: R- COUNTIFS function
body: <p>For each timestamp I would like to count the number of trains/buses with a coordinate higher than the reference.
  See here below the dataset:</p>
  
  <pre><code>structure(list(timestamp = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), type = structure(c(3L, 
  3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 
  1L, 1L, 1L, 1L, 2L), .Label = c("Bus", "Reference", "Train"), class = "factor"), 
      X.coordinate = c(470L, -300L, 25L, 456L, 37L, 19L, 798L, 
      -56L, 489L, 412L, 350L, 278L, 970L, -65L, -894L, 780L, 265L, 
      -25L, 365L, 785L, 95L, 85L)), .Names = c("timestamp", "type", 
  "X.coordinate"), row.names = c(NA, -22L), class = "data.frame")
  </code></pre>
  
  <p>I would like to add 2 columns. One column that gives for each row with "reference" the number of trains with a coordinate higher than the reference. The second column should give the number of buses with a coordinate higher than the reference.</p>
  
  <p>See here below the desired output:</p>
  
  <pre><code> timestamp      type X.coordinate Train_with_higher_X Bus_with_higher_X
  1          1     Train          470                  NA                NA
  2          1     Train         -300                  NA                NA
  3          1     Train           25                  NA                NA
  4          1     Train          456                  NA                NA
  5          1     Train           37                  NA                NA
  6          1       Bus           19                  NA                NA
  7          1       Bus          798                  NA                NA
  8          1       Bus          -56                  NA                NA
  9          1       Bus          489                  NA                NA
  10         1       Bus          412                  NA                NA
  11         1 Reference          350                   2                 3
  12         2     Train          278                  NA                NA
  13         2     Train          970                  NA                NA
  14         2     Train          -65                  NA                NA
  15         2     Train         -894                  NA                NA
  16         2     Train          780                  NA                NA
  17         2       Bus          265                  NA                NA
  18         2       Bus          -25                  NA                NA
  19         2       Bus          365                  NA                NA
  20         2       Bus          785                  NA                NA
  21         2       Bus           95                  NA                NA
  22         2 Reference           85                   3                 4
  </code></pre>
  
  <p>I tried to write some code in R to get this in an elegant way, but my programming skills are too limited for the moment. Is there anybody that knows a solution for this problem? Thank you on beforehand! </p>