# [R] How to do a backward calculation for each record in a dataset

Berend Hasselman bhh at xs4all.nl
Mon Feb 18 21:44:55 CET 2013

```Rolf,

But where is the function newt(…)

Berend

On 18-02-2013, at 21:25, Rolf Turner <rolf.turner at xtra.co.nz> wrote:

>
> Some (quite a few!) years ago I wrote myself a wee function called
> compInt() ("compound interest") to do --- I think --- just what you require.
> I have attached the code for this function and a help file for it.
>
> If anyone else wants this code, and if the attachments don't get through the list,
> let me know and I can send the stuff to you directly.
>
>    cheers,
>
>        Rolf Turner
>
> On 02/18/2013 10:34 PM, Prakasit Singkateera wrote:
>> Hi all,
>>
>> Firstly, it is not a homework. I am working for a hotel booking company in
>> Thailand but I don't want to explain a complex equation and concept here so
>> I keep it simple and closely related to what I am trying to solve.I apology
>> if my question is not clear enough.
>>
>> I am new to R and previously this problem can be solved easily in Excel
>> using the "Goal Seek" tool. An example related to my question is when we
>> use the PMT formula (in Excel) to find the loan payment amount for the
>> given values of parameters i.e. interest rate, total number of payments,
>> and principal amount of the loan.
>>
>> loan_payment_amt_of_each_period =
>> PMT(interest_rate,total_number_of_payments,principal_amt)
>>
>> The question is when you know exactly on a monthly basis that you can
>> afford only X amount of money to pay and you want to know how many months
>> you have to do the payment given your monthly affordable money, the fixed
>> interest rate, and the principal amount of loan. Using Goal Seek tool in
>> Excel, it is like a backward solving for X given Y by not having to
>> transform anything from the original equation. Simply put the
>> loan_payment_amt_of_each_period you want and let the software calculate the
>> total_number_of_payments for you.
>>
>> Thanks arun. But that was you solved the original equation and put it as a
>> new formula to R to calculate the result which is easy as long as the
>> original equation is not complex.
>>
>>
>> Thanks you,
>> Prakasit Singkateera
>>
>>
>> On Mon, Feb 18, 2013 at 1:18 AM, Bert Gunter <gunter.berton at gene.com> wrote:
>>
>>> Homework? We don't do homework here.
>>>
>>> -- Bert
>>>
>>> On Sun, Feb 17, 2013 at 5:10 AM, Prakasit Singkateera
>>> <asltjoey.rsoft at gmail.com> wrote:
>>>> Hi Experts,
>>>>
>>>> I have a dataset of 3 columns:
>>>>
>>>> customer.name     product     cost
>>>> John     Toothpaste     30
>>>> Mike     Toothpaste     45
>>>> Peter     Toothpaste     40
>>>>
>>>> And I have a function of cost whereby
>>>>
>>>> cost = 3.40 + (1.20 * no.of.orders^2)
>>>>
>>>> I want to do a backward calculation for each records (each customer) to
>>>> find his no.of.orders and create a new column named "no.of.orders" in
>>> that
>>>> dataset but I don't know how to do.
>>>>
>>>>
>>>> Thank you everyone,
>>>> Prakasit
>>>
>>>
>>> --
>>>
>>> Bert Gunter
>>> Genentech Nonclinical Biostatistics
>>>
>>> Internal Contact Info:
>>> Phone: 467-7374
>>> Website:
>>>