File: rmClose.r

package info (click to toggle)
hmisc 5.2-4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,044 kB
  • sloc: asm: 28,905; f90: 590; ansic: 415; xml: 160; fortran: 75; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,272 bytes parent folder | download | duplicates (2)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#' rmClose
#' 
#' Remove close values from a numeric vector that are not at the outer limtis.  This is useful for removing axis breaks that overlap when plotting.
#'
#' @param x a numeric vector with no `NA`s
#' @param minfrac minimum allowed spacing between consecutive ordered `x`, as a fraction of the range of `x`
#'
#' @returns a sorted numeric vector of non-close values of `x`
#' @export
#' @md
#' @author Frank Harrell
#' @examples
#' rmClose(c(1, 2, 4, 47, 48, 49, 50), minfrac=0.07)
rmClose <- function(x, minfrac=0.05) {
  x <- unique(sort(x))
  mindist <- minfrac * diff(range(x)) 
  n <- length(x)
  selected      <- x[1]
  last_selected <- x[1]  # Always select the first point
  
  for (i in 2 : n) {
    if (x[i] - last_selected >= mindist) {
      selected <- c(selected, x[i])
      last_selected <- x[i]
    }
  }
  return(selected)
}

# Code not used:
 # repeat {
 #   prn(x)
 #   n <- length(x)
 #   if(n < 3) return(x)
 #   if(x[n - 1] > x[n] - mindist) {
 #     x <- x[- (n - 1)]
 #     next
 #   }
 #   gaps <- diff(c(- mindist * 2, x))
 #   mind <- min(gaps[- c(1, n)])
 #   prn(gaps); prn(mind)
 #   if(mind >= mindist) return(x)
 #   candidates <- setdiff(which(gaps == mind), c(1, n))
 #   prn(candidates)
 #   x <- x[- candidates[1]]
 # }
 #}