for example, I have 8 couple payment periods. for each row , I would like to get a present value. should I use tapply? sapply? would you please show how I should deal with it. thanks in advance enter image description here
Asked
Active
Viewed 28 times
0
-
Please include an example. – Kristofersen Mar 15 '17 at 15:19
-
row is the interest rate, coupon payment =40, and the last payment = 1040. there are 8 periods. I would like to get a present value for each row – HKU_Johnny_QFin Mar 15 '17 at 15:22
-
1Don't post pictures of data; include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). How do you determine "present value." Show the desired ouput. – MrFlick Mar 15 '17 at 15:22
-
row is the interest rate, coupon payment =40, and the last payment = 1040. there are 8 periods. I would like to get a present value for each row – HKU_Johnny_QFin Mar 15 '17 at 15:22
-
@HKU_Johnny_QFin that's easy for you to follow since you've seen the data but it's much harder to imagine. Please just include the first few rows of your data and an example of the output you'd like to see and it will be much easier to help you. – Kristofersen Mar 15 '17 at 15:23
-
present value = 40/(1+first interest rate)^0.5 + 40/(1+second interest rate)^1. + 40 /(1+third i.r)^1.5 .....+40/(1+7th ir)^3.5 +1040/(1+8th ir)^4 – HKU_Johnny_QFin Mar 15 '17 at 15:24
-
@MrFlick plz see the above description, sorry I am a newb – HKU_Johnny_QFin Mar 15 '17 at 15:28
1 Answers
0
I'm assuming your data looks like this:
IR1 IR2 IR3 IR4 IR5 IR6 IR7 IR8 Coupon lastPayment
# # # # # # # # 40 1040
Let your data be called dat. Then, you can find your presentvalue by running this.
dat$presentvalue = dat$Coupon*(1/(1+dat$IR1)^(.5) + 1/(1+dat$IR2) + 1/(1 + dat$IR3)^(3/2) + ... + 1/(1 + dat$IR7)^(3.5)) + dat$lastPayment/(1+dat$IR8)^4
A function example:
doStuff = function(dat){
dat$presentvalue = dat$Coupon*(1/(1+dat$IR1)^(.5) + 1/(1+dat$IR2) + 1/(1 + dat$IR3)^(3/2) + ... + 1/(1 + dat$IR7)^(3.5)) + dat$lastPayment/(1+dat$IR8)^4
return(dat)
}
Since this is for a simulation you're really going to want to run something like this.
for(i in 1:nSim){
dat = generateDat
dat$presentvalue = dat$Coupon*(1/(1+dat$IR1)^(.5) + 1/(1+dat$IR2) + 1/(1 + dat$IR3)^(3/2) + ... + 1/(1 + dat$IR7)^(3.5)) + dat$lastPayment/(1+dat$IR8)^4
}
You don't need a function to calculate the presentvalue. the simulation will be setup the same everytime so just add it in the for loop below where you generate your data.

Kristofersen
- 2,736
- 1
- 15
- 31
-
thank you very much for helping me. output 'Error in dat$Coupon : $ operator is invalid for atomic vectors – HKU_Johnny_QFin Mar 15 '17 at 15:47
-
-
@HKU_Johnny_QFin Cool, well if everything is working please mark it as closed. – Kristofersen Mar 15 '17 at 15:52
-
with all due respect, I need to run 50 rows of data, i was expecting a function or a for loop. what you provided is similar to with I did previously. I expected function or tapply or sapply etc. thank you all the same – HKU_Johnny_QFin Mar 15 '17 at 15:52
-
@HKU_Johnny_QFin running it this way will solve it for all rows and be much faster than a for loop – Kristofersen Mar 15 '17 at 15:53
-
@HKU_Johnny_QFin is it running slower than you were hoping for? or is something not working with the way the code is written now? – Kristofersen Mar 15 '17 at 15:54
-
thanks for the follow-up, I want to have somehow a summation function to help me. because this function may apple to even more numbers. thank you very much. – HKU_Johnny_QFin Mar 15 '17 at 15:57
-
@HKU_Johnny_QFin include an example of what you want the output to be. I apparently cant follow what you are asking, but if you include an example i can code it for you or help you code it. – Kristofersen Mar 15 '17 at 15:58
-
the previous example is Sum(i.r = v1 to v8) [1/(1+i.r)^(0.5i)] *40+1040 /(1+ 8th ir)^(8*0.5) – HKU_Johnny_QFin Mar 15 '17 at 16:02
-
@HKU_Johnny_QFin that doesn't help. include a sample of your data and what you want the output to be in each row. – Kristofersen Mar 15 '17 at 16:03
-
-
the ouput you provided is right, I mean I want to have a function to do this instead of wiring a long code. thank you very much. – HKU_Johnny_QFin Mar 15 '17 at 16:04
-
@HKU_Johnny_QFin do you have multiple dataframes you need to run this code on? or why do you want a function? – Kristofersen Mar 15 '17 at 16:04
-
@HKU_Johnny_QFin if all column names are the same, then the function i added will work. – Kristofersen Mar 15 '17 at 16:06
-
later , I need to construct a 'duration calculation" in R. methodology is quite similar, and I would like to mimic the function – HKU_Johnny_QFin Mar 15 '17 at 16:07
-