-6

How do I write a function that multiplies a number based upon the content of another variable unless a specific total is reached?

x*1.45 until it reaches 1000, and if it reaches 1000, then it stops.

Edit:

For further context, this what I am trying to calculate the number of individuals in poverty given the following:

For families with children under age 6, the proposal would phase in the CTC at a rate of 45 cents per dollar of earnings starting with the first dollar of earnings.

For families with older children, the phase in would be 15 cents per dollar of earnings.

Below is what I have currently, but it isn't quite right:

y <-
    svrepdesign(
        weights = ~marsupwt, 
        repweights = "pwwgt[1-9]", 
        type = "Fay", 
        rho = (1-1/sqrt(4)),
        data = "asec15" ,
        combined.weights = T ,
        dbtype = "MonetDBLite" ,
        dbname = dbfolder
    )

    f = function(x) if (x < 1000) exp(seq(log(x), log(1000), by=log(1.15))) else x
    c = function(x) if (x < 2000) exp(seq(log(x), log(2000), by=log(1.45))) else x

CTC_Update <-subset( 
        y , 
            (spmu_numkids == 0 & spmu_resources < spmu_povthreshold) | 
            (spmu_numkids == 1 & fownu6 == 0 & spmu_resources - spmu_actc + 1*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 2 & fownu6 == 0 & spmu_resources - spmu_actc + 2*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 3 & fownu6 == 0 & spmu_resources - spmu_actc + 3*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 4 & fownu6 == 0 & spmu_resources - spmu_actc + 4*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 5 & fownu6 == 0 & spmu_resources - spmu_actc + 5*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 6 & fownu6 == 0 & spmu_resources - spmu_actc + 6*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 7 & fownu6 == 0 & spmu_resources - spmu_actc + 7*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 8 & fownu6 == 0 & spmu_resources - spmu_actc + 8*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 9 & fownu6 == 0 & spmu_resources - spmu_actc + 9*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 10 & fownu6 == 0 & spmu_resources - spmu_actc + 10*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 11 & fownu6 == 0 & spmu_resources - spmu_actc + 11*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 12 & fownu6 == 0 & spmu_resources - spmu_actc + 12*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 13 & fownu6 == 0 & spmu_resources - spmu_actc + 13*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 14 & fownu6 == 0 & spmu_resources - spmu_actc + 14*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 15 & fownu6 == 0 & spmu_resources - spmu_actc + 15*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 16 & fownu6 == 0 & spmu_resources - spmu_actc + 16*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 17 & fownu6 == 0 & spmu_resources - spmu_actc + 17*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 18 & fownu6 == 0 & spmu_resources - spmu_actc + 18*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 19 & fownu6 == 0 & spmu_resources - spmu_actc + 19*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) |
            (spmu_numkids >= 20 & fownu6 == 0 & spmu_resources - spmu_actc + 20*f(ftotval) + 0*c(ftotval) < spmu_povthreshold) |
            (spmu_numkids == 1 & fownu6 == 1 & spmu_resources - spmu_actc + 1*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 2 & fownu6 == 1 & spmu_resources - spmu_actc + 2*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 3 & fownu6 == 1 & spmu_resources - spmu_actc + 3*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 4 & fownu6 == 1 & spmu_resources - spmu_actc + 4*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 5 & fownu6 == 1 & spmu_resources - spmu_actc + 5*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 6 & fownu6 == 1 & spmu_resources - spmu_actc + 6*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 7 & fownu6 == 1 & spmu_resources - spmu_actc + 7*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 8 & fownu6 == 1 & spmu_resources - spmu_actc + 8*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 9 & fownu6 == 1 & spmu_resources - spmu_actc + 9*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 10 & fownu6 == 1 & spmu_resources - spmu_actc + 10*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 11 & fownu6 == 1 & spmu_resources - spmu_actc + 11*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 12 & fownu6 == 1 & spmu_resources - spmu_actc + 12*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 13 & fownu6 == 1 & spmu_resources - spmu_actc + 13*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 14 & fownu6 == 1 & spmu_resources - spmu_actc + 14*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 15 & fownu6 == 1 & spmu_resources - spmu_actc + 15*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 16 & fownu6 == 1 & spmu_resources - spmu_actc + 16*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 17 & fownu6 == 1 & spmu_resources - spmu_actc + 17*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 18 & fownu6 == 1 & spmu_resources - spmu_actc + 18*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 19 & fownu6 == 1 & spmu_resources - spmu_actc + 19*f(ftotval) + 1*c(ftotval) < spmu_povthreshold) |
            (spmu_numkids >= 20 & fownu6 == 1 & spmu_resources - spmu_actc + 20*f(ftotval) + 1*c(ftotval) < spmu_povthreshold)  |       
            (spmu_numkids == 1 & fownu6 == 2 & spmu_resources - spmu_actc + 1*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 2 & fownu6 == 2 & spmu_resources - spmu_actc + 2*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 3 & fownu6 == 2 & spmu_resources - spmu_actc + 3*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 4 & fownu6 == 2 & spmu_resources - spmu_actc + 4*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 5 & fownu6 == 2 & spmu_resources - spmu_actc + 5*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 6 & fownu6 == 2 & spmu_resources - spmu_actc + 6*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 7 & fownu6 == 2 & spmu_resources - spmu_actc + 7*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 8 & fownu6 == 2 & spmu_resources - spmu_actc + 8*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 9 & fownu6 == 2 & spmu_resources - spmu_actc + 9*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 10 & fownu6 == 2 & spmu_resources - spmu_actc + 10*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 11 & fownu6 == 2 & spmu_resources - spmu_actc + 11*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 12 & fownu6 == 2 & spmu_resources - spmu_actc + 12*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 13 & fownu6 == 2 & spmu_resources - spmu_actc + 13*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 14 & fownu6 == 2 & spmu_resources - spmu_actc + 14*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 15 & fownu6 == 2 & spmu_resources - spmu_actc + 15*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 16 & fownu6 == 2 & spmu_resources - spmu_actc + 16*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 17 & fownu6 == 2 & spmu_resources - spmu_actc + 17*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 18 & fownu6 == 2 & spmu_resources - spmu_actc + 18*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 19 & fownu6 == 2 & spmu_resources - spmu_actc + 19*f(ftotval) + 2*c(ftotval) < spmu_povthreshold) |
            (spmu_numkids >= 20 & fownu6 == 2 & spmu_resources - spmu_actc + 20*f(ftotval) + 2*c(ftotval) < spmu_povthreshold)  |   
            (spmu_numkids == 1 & fownu6 == 3 & spmu_resources - spmu_actc + 1*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 2 & fownu6 == 3 & spmu_resources - spmu_actc + 2*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 3 & fownu6 == 3 & spmu_resources - spmu_actc + 3*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 4 & fownu6 == 3 & spmu_resources - spmu_actc + 4*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 5 & fownu6 == 3 & spmu_resources - spmu_actc + 5*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 6 & fownu6 == 3 & spmu_resources - spmu_actc + 6*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 7 & fownu6 == 3 & spmu_resources - spmu_actc + 7*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 8 & fownu6 == 3 & spmu_resources - spmu_actc + 8*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 9 & fownu6 == 3 & spmu_resources - spmu_actc + 9*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 10 & fownu6 == 3 & spmu_resources - spmu_actc + 10*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 11 & fownu6 == 3 & spmu_resources - spmu_actc + 11*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 12 & fownu6 == 3 & spmu_resources - spmu_actc + 12*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 13 & fownu6 == 3 & spmu_resources - spmu_actc + 13*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 14 & fownu6 == 3 & spmu_resources - spmu_actc + 14*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 15 & fownu6 == 3 & spmu_resources - spmu_actc + 15*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 16 & fownu6 == 3 & spmu_resources - spmu_actc + 16*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 17 & fownu6 == 3 & spmu_resources - spmu_actc + 17*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 18 & fownu6 == 3 & spmu_resources - spmu_actc + 18*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 19 & fownu6 == 3 & spmu_resources - spmu_actc + 19*f(ftotval) + 3*c(ftotval) < spmu_povthreshold) |
            (spmu_numkids >= 20 & fownu6 == 3 & spmu_resources - spmu_actc + 20*f(ftotval) + 3*c(ftotval) < spmu_povthreshold)  |   
            (spmu_numkids == 1 & fownu6 == 4 & spmu_resources - spmu_actc + 1*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 2 & fownu6 == 4 & spmu_resources - spmu_actc + 2*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 3 & fownu6 == 4 & spmu_resources - spmu_actc + 3*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 4 & fownu6 == 4 & spmu_resources - spmu_actc + 4*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 5 & fownu6 == 4 & spmu_resources - spmu_actc + 5*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 6 & fownu6 == 4 & spmu_resources - spmu_actc + 6*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 7 & fownu6 == 4 & spmu_resources - spmu_actc + 7*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 8 & fownu6 == 4 & spmu_resources - spmu_actc + 8*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 9 & fownu6 == 4 & spmu_resources - spmu_actc + 9*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 10 & fownu6 == 4 & spmu_resources - spmu_actc + 10*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 11 & fownu6 == 4 & spmu_resources - spmu_actc + 11*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 12 & fownu6 == 4 & spmu_resources - spmu_actc + 12*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 13 & fownu6 == 4 & spmu_resources - spmu_actc + 13*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 14 & fownu6 == 4 & spmu_resources - spmu_actc + 14*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 15 & fownu6 == 4 & spmu_resources - spmu_actc + 15*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 16 & fownu6 == 4 & spmu_resources - spmu_actc + 16*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 17 & fownu6 == 4 & spmu_resources - spmu_actc + 17*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 18 & fownu6 == 4 & spmu_resources - spmu_actc + 18*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 19 & fownu6 == 4 & spmu_resources - spmu_actc + 19*f(ftotval) + 4*c(ftotval) < spmu_povthreshold) |
            (spmu_numkids >= 20 & fownu6 == 4 & spmu_resources - spmu_actc + 20*f(ftotval) + 4*c(ftotval) < spmu_povthreshold)  |               
            (spmu_numkids == 1 & fownu6 == 5 & spmu_resources - spmu_actc + 1*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 2 & fownu6 == 5 & spmu_resources - spmu_actc + 2*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 3 & fownu6 == 5 & spmu_resources - spmu_actc + 3*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 4 & fownu6 == 5 & spmu_resources - spmu_actc + 4*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 5 & fownu6 == 5 & spmu_resources - spmu_actc + 5*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 6 & fownu6 == 5 & spmu_resources - spmu_actc + 6*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 7 & fownu6 == 5 & spmu_resources - spmu_actc + 7*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 8 & fownu6 == 5 & spmu_resources - spmu_actc + 8*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 9 & fownu6 == 5 & spmu_resources - spmu_actc + 9*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 10 & fownu6 == 5 & spmu_resources - spmu_actc + 10*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 11 & fownu6 == 5 & spmu_resources - spmu_actc + 11*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 12 & fownu6 == 5 & spmu_resources - spmu_actc + 12*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 13 & fownu6 == 5 & spmu_resources - spmu_actc + 13*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 14 & fownu6 == 5 & spmu_resources - spmu_actc + 14*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 15 & fownu6 == 5 & spmu_resources - spmu_actc + 15*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 16 & fownu6 == 5 & spmu_resources - spmu_actc + 16*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 17 & fownu6 == 5 & spmu_resources - spmu_actc + 17*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 18 & fownu6 == 5 & spmu_resources - spmu_actc + 18*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 19 & fownu6 == 5 & spmu_resources - spmu_actc + 19*f(ftotval) + 5*c(ftotval) < spmu_povthreshold) |
            (spmu_numkids >= 20 & fownu6 == 5 & spmu_resources - spmu_actc + 20*f(ftotval) + 5*c(ftotval) < spmu_povthreshold)  |   
            (spmu_numkids == 1 & fownu6 == 6 & spmu_resources - spmu_actc + 1*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 2 & fownu6 == 6 & spmu_resources - spmu_actc + 2*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 3 & fownu6 == 6 & spmu_resources - spmu_actc + 3*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 4 & fownu6 == 6 & spmu_resources - spmu_actc + 4*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 5 & fownu6 == 6 & spmu_resources - spmu_actc + 5*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 6 & fownu6 == 6 & spmu_resources - spmu_actc + 6*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 7 & fownu6 == 6 & spmu_resources - spmu_actc + 7*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 8 & fownu6 == 6 & spmu_resources - spmu_actc + 8*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 9 & fownu6 == 6 & spmu_resources - spmu_actc + 9*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 10 & fownu6 == 6 & spmu_resources - spmu_actc + 10*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 11 & fownu6 == 6 & spmu_resources - spmu_actc + 11*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 12 & fownu6 == 6 & spmu_resources - spmu_actc + 12*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 13 & fownu6 == 6 & spmu_resources - spmu_actc + 13*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 14 & fownu6 == 6 & spmu_resources - spmu_actc + 14*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 15 & fownu6 == 6 & spmu_resources - spmu_actc + 15*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 16 & fownu6 == 6 & spmu_resources - spmu_actc + 16*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 17 & fownu6 == 6 & spmu_resources - spmu_actc + 17*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 18 & fownu6 == 6 & spmu_resources - spmu_actc + 18*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) | 
            (spmu_numkids == 19 & fownu6 == 6 & spmu_resources - spmu_actc + 19*f(ftotval) + 6*c(ftotval) < spmu_povthreshold) |
            (spmu_numkids >= 20 & fownu6 == 6 & spmu_resources - spmu_actc + 20*f(ftotval) + 6*c(ftotval) < spmu_povthreshold))
Robert Orr
  • 13
  • 2
  • You should make yourself familiar with loops. I recommend googling "control statements" plus the language of your choice. :) – fxvdh Oct 13 '16 at 22:00
  • 1
    It's good to include your code, but contrary to what a more obnoxious commenter said before, it's also important that we be able to run the code. Generally, the best way to go is to make a minimal reproducible example that illustrates the central problem, instead of giving the full code for your project. Some guidance is here: http://stackoverflow.com/a/28481250/ – Frank Oct 14 '16 at 14:44
  • 1
    Agree with @Frank - as you can see, given enough information *most* people here will make a real effort to help. But we can only work with what we are given. Speaking for myself, while I appreciate your efforts to try to improve the clarity of the question, I'm actually findng it hardert now to comprehend exactly what your problem is. Do read the link Frank gave, carefully, then take some time to make the question as good as you can. Then you'll reap the rewards in good answers – dww Oct 14 '16 at 15:17
  • you probably want `pmin` somewhere in your calculations. `pmin( 1000 , c( 10 , 100 , 1000 , 10000 , 100000 ) )` – Anthony Damico Oct 17 '16 at 11:23

4 Answers4

5

I'd do...

f = function(x) if (x > 1000) exp(seq(log(x), log(1000), by=log(.45))) else x

# example
f(100000)
[1] 100000.000  45000.000  20250.000   9112.500   4100.625   1845.281

seq(from, to, by) will not pass to (showing the next number past 1000), but if you want that, just add it onto the end of the vector (dividing by .45 again).

The exp and log business comes from..

log(x*.45^y) = log(x) + y*log(.45)
Frank
  • 66,179
  • 8
  • 96
  • 180
3

This is a well suited task for a recursive function:

f <- function(x, coeff, threshold){
    t <- tail(x,1)
    if (t*coeff<threshold)
        return(x)
    f(c(x, t*coeff), coeff, threshold)
}

f(x = 100000, coeff = 0.45, threshold = 1000)

#[1] 100000.000  45000.000  20250.000   9112.500   4100.625   1845.281
989
  • 12,579
  • 5
  • 31
  • 53
  • Thanks for your help, but I didn't quite explain the situation adequately. I updated my post. Would you mind taking another look? – Robert Orr Oct 14 '16 at 13:45
2

Is this what you want?

MultiplyUntil <- function(x,y,threshhold) {
    repeat
    {
        print(x)
        x <- x * y
        if (x < threshhold)
        {
            print(x)
            break
        }
        if (x > threshhold & y > 1)
        {
            print("Will go to infinity")
            break
        }
    }
}

In your given example x <- x, y <- .45 and threshhold <- 1000:

# with x = 10000
MultiplyUntil(10000, .45, 1000)
# [1] 10000
# [1] 4500
# [1] 2025
# [1] 911.25
Frank
  • 66,179
  • 8
  • 96
  • 180
Zach
  • 1,103
  • 8
  • 11
2

First, we define a function that multiplies a given start value by a ratio r (to create a geometric series) as many times as we can without passing a given threshold value

f = function(start, r, threshold){
  N = floor(log(threshold/start) / log(r))
  r^(0:N) * start
}

Now, to apply this to your example of

"x*1.45 until it reaches 1000, and if it reaches 1000, then it stops."

For a starting value (x) of, say 10, we can do

x = 10    
f(x, 1.45, 1000)
# [1]  10.00000  14.50000  21.02500  30.48625  44.20506  64.09734  92.94114
# [8] 134.76466 195.40876 283.34269 410.84691 595.72802 863.80562

In case you also wanted to include the next value in the series, which is the fist value to exceed the threshold, you would use ceiling intead of floor in the function.

dww
  • 30,425
  • 5
  • 68
  • 111