Creating my own function

I figured it out thanks for the help everyone

The problem is here, the dimension of your x parameter has to be == to the dimmension of your times parameter or a scalar value.

bond_value <- 1070.919
r <- 0.07
ttm <- seq(1, 4, 1)
rep(x = bond_value * r, times = ttm - 1)
#> Error in rep(x = bond_value * r, times = ttm - 1): invalid 'times' argument
rep(x = 1:4, times = ttm - 1)
#> [1] 2 3 3 4 4 4

Created on 2019-04-16 by the reprex package (v0.2.1.9000)

andresrcs,

So is what you are saying I have to get my x value to equal my times value?

What I am confused on is how my parameter now equals [1] 2 3 3 4 4 4, but on the first function it worked fine... is it because the second set of data is a sequence, maybe I am just confused on the sequence function...

I honestly don't understand what are you trying to accomplish, so I don't know if you "have to". I'm just saying that you can't use the 'rep()' function this way, you can't repeat a scalar value by times "vector" .

bond_value <- 1070.919
r <- 0.07
ttm <- seq(1, 4, 1)

rep(x = bond_value * r, times = ttm - 1)
#> Error in rep(x = bond_value * r, times = ttm - 1): invalid 'times' argument

# Your x parameter would be a scalar value
bond_value * r
#> [1] 74.96433

# Your times parameter would be a vector
ttm - 1
#> [1] 0 1 2 3

# This is equivalent to
rep(x = 74.96433, times = c(0, 1, 2, 3))
#> Error in rep(x = 74.96433, times = c(0, 1, 2, 3)): invalid 'times' argument

I basically am trying to write my own function for the par value of a bond similar to my own function i wrote for the bond value above....

bondprice<- function(p, r, ttm, y) {
cf <- c(rep(p * r, ttm - 1), p * (1 + r))
cf <- data.frame(cf)
cf$t <- as.numeric(rownames(cf))
cf$pv_factor <- 1 / (1 + y)^cf$t
cf$pv <- cf$cf * cf$pv_factor
sum(cf$pv)
}

and then my final goal is to make sure my function works correct by running bondprice(p, r, ttm, y) with my values above...

I guess I am just writing my Par formula wrong and am not fully grasping this darn sequence on how to get my numbers in my formula correct, sorry if i making this confusing... I am fairly new to R and my wording and way of descrivbing might be bad...

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.