Re: [R] "spreading out" a numeric vector

From: Levi Waldron <leviwaldron_at_gmail.com>
Date: Sat, 22 Mar 2008 23:10:24 -0400

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.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 23 Mar 2008 - 03:13:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 23 Mar 2008 - 03:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive