Re: [R] Group close numbers in a vector

From: Salih Tuna <salihtuna_at_gmail.com>
Date: Sun, 22 May 2011 11:36:35 +0100

Hi Jim,
I think i sorted it out how to read and write each vector separately. Thanks a lot. It was exactly what i wanted to do. best,
salih

On Sat, May 21, 2011 at 11:41 PM, jim holtman <jholtman_at_gmail.com> wrote:

> Is this what you are after:
>
> > x = c(1 ,2 ,4 ,7 ,9 ,10 ,15)
> > # partition if the difference is > 2)
> > breaks <- cumsum(c(0, diff(x) > 2))
> > # partition into different lists
> > split(x, breaks)
> $`0`
> [1] 1 2 4
>
> $`1`
> [1] 7 9 10
>
> $`2`
> [1] 15
>
>
>
> On Sat, May 21, 2011 at 6:03 PM, Salih Tuna <salihtuna_at_gmail.com> wrote:
> > Hi Robert,
> > thanks for your reply. is there a way to store them in separate vectors?
> > and when i try it with a different example i got different result. For
> > example if x = [1 2 8 9]
> > i want the result to be x1 = [1 2] and x2 = [8 9].
> > thanks
> >
> > On Sat, May 21, 2011 at 7:16 PM, Robert Baer <rbaer_at_atsu.edu> wrote:
> >
> >> Hi everyone,
> >>> i am trying to group close numbers in a vector.
> >>> For example i have a vector x = [1 2 4 7 9 10 15].
> >>> I want the code to pick 1 2 4 (max difference between successive
> numbers
> >>> is
> >>> 2) and assign them to variable a, then pick 7 9 10 and assign them to b
> >>> and
> >>> 15 to c. But since i do not know how many groups there will be the code
> >>> should create a,b,c etc as it goes along. So if x = [1 2 4 7 9 10 15
> 20]
> >>> it
> >>> should create a,b,c and d this time and assign 20 to d (while the
> others
> >>> remain the same).
> >>>
> >>
> >> 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
> >>
> >>
> >>
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
> >
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
>

        [[alternative HTML version deleted]]



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 22 May 2011 - 10:40:16 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

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 22 May 2011 - 12:20:09 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