[R] (Fwd) Re: Breakdown a number

Petr Pikal petr.pikal at precheza.cz
Thu Apr 20 17:24:09 CEST 2006


------- Forwarded message follows -------
From:           	Petr Pikal <petr.pikal at precheza.cz>
To:             	roebuck at mdanderson.org
Subject:        	Re: [R] Breakdown a number
Date sent:      	Thu, 20 Apr 2006 17:22:03 +0200

Hi

try this

fff<-function(x,breaks=c(5,10)) {
if (sum(breaks<x)==0)
result <- c(x,rep(0,length(breaks)))
else
result <- c(breaks[breaks<=x],x-max(breaks[breaks<=x]))
result
}

If you want trailing zeroes you need to add them.

HTH
Petr

Paul Roebuck 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

----------------------------------------------------------

SIGSIG -- signature too long (core dumped) 
------- End of forwarded message -------Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list