[R] "spreading out" a numeric vector

Levi Waldron leviwaldron at gmail.com
Sun Mar 23 04:10:24 CET 2008


On Sat, Mar 22, 2008 at 9:58 PM, jim holtman <jholtman at gmail.com> wrote:
> Is this close to what you want?
>
>  > spread <- function(x, mindiff=0.5){
>  +     unique(as.integer(x / mindiff) * mindiff)
>  + }
>
> >
>  > x <- c(1,2,seq(2.1,2.3,0.1),3,4)
>  > x
>  [1] 1.0 2.0 2.1 2.2 2.3 3.0 4.0
>  > spread(x)
>  [1] 1 2 3 4
>  >
>  > spread(x,.2)
>  [1] 1.0 2.0 2.2 3.0 4.0
>

There are a couple problems with this solution:
   1.  The main problem is that it causes closely spaced numbers to
vanish, rather than spread out
   2.  The vectors I'm actually working with are large integers, for example:
> x
 [1]   342951  1491880  5447637  6335019  7368604  8960148 10635802 16907621
 [9] 18071422 18747461 19379960 21767641 21850851 23143537 26693431 35298190

with mindiff = 500000.  I could scale these numbers to make your
solution work, except for problem #1.

Interesting idea though.



More information about the R-help mailing list