I have some inputs
#Input
C<-0.05 ## Coupon rate
y <-0.07 ## Yield to maturity
m <-5 ## Maturity
And I wish to calculate
rep= c(rep(0,m-1),1)
rep<- rep+C
df<-data.frame(T = 1:m)
df$CF <- rep
df<- data.frame(df)
df$DF <- 1/(1+y)^(df$T)
df$DC <- df$DF*df$CF
P0 <- sum(df$DC) # initial bond price
df$TDC <-df$T*df$DC # time * discounted CF
STDC <- sum(df$TDC)
The final output I wish to get is this
D_mac_dk1 <- STDC/P0
How can I create a loop if I wish to run a few inputs at once? For example, if I have multiple c,y and m. Thank you.