[R] Breakdown a number
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Apr 20 06:50:11 CEST 2006
On 4/19/06, Paul Roebuck <roebuck at mdanderson.org> wrote:
> On Wed, 19 Apr 2006, Gabor Grothendieck wrote:
>
> > On 4/19/06, Paul Roebuck <roebuck at mdanderson.org> wrote:
> >
> > > Isn't there a builtin method for doing this and, if so,
> > > what is it called?
> > >
> > > breakdown <- function(whole) {
> > > breaks <- c(250, 800)
> > > pieces <- integer(length(breaks) + 1)
> > > if (whole > breaks[2]) {
> > > pieces[3] <- whole - breaks[2]
> > > whole <- breaks[2]
> > > }
> > > if (whole > breaks[1]) {
> > > pieces[2] <- whole - breaks[1]
> > > whole <- breaks[1]
> > > }
> > > pieces[1] <- whole
> > >
> > > return(pieces)
> > > }
> > >
> > > breakdown(1200) # 250 550 400
> >
> > Maybe you could discuss in words what you want but perhaps
> > you are looking for diff:
>
> That was rather my problem searching for my answer as I
> was unsure what this was called. I was searching for
> 'bins' or 'breaks' (like in hist method but not for
> plotting) and leading nowhere fast. Alas, I thought
> sample code would go further than my description.
>
>
> > > bp <- c(0, 250, 800, 1200)
> > > diff(bp)
> > [1] 250 550 400
>
> Don't think diff method is going to work either, at
> least in cases where the 'whole' is less than greatest
> 'break'.
>
> > breakdown2 <- function(x, breaks = c(250, 800)) {
> diff(c(0, breaks, x))
> }
> > breakdown(10) # 10 0 0
> > breakdown2(10) # 250 550 -790
> >
> > breakdown(400) # 250 150 0
> > breakdown2(400) # 250 550 -400
>
Just sort it:
bd <- function(x, breaks = c(250, 800)) diff(sort(c(0, breaks, x)))
If you don't want the 0 values in the case that x is one of the
breaks then use diff(sort(unique(c(0, breaks, x))))
More information about the R-help
mailing list