Skip to content
Prev 260535 / 398502 Next

Group close numbers in a vector

I think the following function should do basically what you want:
codeit = function(x){
  action = c(TRUE, diff(x) <= 2)
  g = paste('x', '1', sep='')
  j = 1
  for (i in 2: length(x)){
    if (action[i-1] != action[i] | action[i] == FALSE) j = j+1
      g[i] = paste('x', j, sep='')
  }
  df = data.frame(x, grp.x=g)
}




------------------------------------------
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A. T. Still University of Health Sciences
800 W. Jefferson St.
Kirksville, MO 63501
660-626-2322
FAX 660-626-2965