[R] irregular sequence of events

JS Huang js.huang at protective.com
Sat Feb 21 00:24:38 CET 2015


Hi,

  Herr is one implementation with function named eventList.

> start
[1]  5 13 21
> start
[1]  5 13 21
> end
[1] 10 16 27
> x
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30
> eventList
function(start, end, x)
{
  result <- character(0)
  for (i in 1:length(start))
  {
    if (i == 1)
    {
      if (start[i] > 1)
      {
        result <- c(result, rep(NA, start[i] - 1))
      }
      result <- c(result, rep(paste0("A",i), end[i] - start[i] + 1))
    }
    else
    {
      if (start[i] > end[i - 1] + 1)
      {
        result <- c(result, rep(NA, start[i] - end[i - 1] - 1))
      }
      result <- c(result, rep(paste0("A", i), end[i] - start[i] + 1))
    }
  }
  if (end[length(start)] < length(x))
  {
    result <- c(result, rep(NA, length(x) - end[length(start)]))
  }
  return(result)
}
> eventList(start, end, x)
 [1] NA   NA   NA   NA   "A1" "A1" "A1" "A1" "A1" "A1" NA   NA   "A2" "A2"
"A2" "A2" NA   NA   NA  
[20] NA   "A3" "A3" "A3" "A3" "A3" "A3" "A3" NA   NA   NA  
> 



--
View this message in context: http://r.789695.n4.nabble.com/irregular-sequence-of-events-tp4703579p4703624.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list