# Daily yield to maturity using uniroot in R: error

So, I'm trying to compute the daily yielt to maturity on basis of data retrieved from Datastream. The data comprises EMU Treasury bonds with Prices, Coupon and Maturity date. In R the matrices are constructed that daily all prices, coupons and time-to maturites are arranged corresponding to their date. When matured, all three variables are 0 (zero).

Using the bondvalue function:

bondvalue = function(c,T,r,par)  { bv = c/r + (par - c/r) * (1+r)^(-2*T) bv }


I essentially use:

Yields = c(Yields,  uniroot(function(r) bondvalue(Coupon*100, Prices, r, 100) - Prices,  c(-1, 1))$root)  to find yield to maturities. However, as stated before I would like to compute daily yield to maturities. Therefore I use a for loop: daily_ytm <- matrix(NA,ncol = ncol(Prices), nrow = nrow(Prices)) for (i in 1:nrow(Prices)) { for (j in 1:ncol(Prices)) { if (Prices[i,j] == 0){ daily_ytm[i,j]=0 } else if (Prices > 0){ daily_ytm[i,j]=c(daily_ytm, uniroot(function(r) bondvalue(Coupon*100, Prices, r, 100) - Prices, c(-1, 1))$root)
}
}
}


Where I would like to get a 0 (zero) in the new matrix, whenever the Price/Coupon/Maturity is zero (or NA originally), otherwise it should find the ytm using uniroot().

>Error in pmax.int(pmin(x, .Machine$double.xmax), -.Machine$double.xmax) :  invalid input type In addition: Warning messages:
>1: In if (Prices > 0) { : the condition has length > 1 and only the first element will be used
>2: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") : the condition has length > 1 and only the first element will be used
>3: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") : the condition has length > 1 and only the first element will be used
>4: In cbind(is.na(mmm), is.na(each)) : number of rows of result is not a multiple of vector length (arg 2)


What am I doing wrong here?

> head(Prices)
PTOTEAOE0013 PTOTEMOE0001 DE0004109103 PTOTEOOE0009
1999-12-31       105.88       100.13      120.033        108.4
2000-01-03       105.75       100.13      120.033        108.4
2000-01-04       105.85       100.13      120.033        108.4
2000-01-05       105.92       100.13      120.033        108.4
2000-01-06       105.92       100.13      120.033        108.4
2000-01-07       106.20       100.13      120.033        108.4

PTOTEUOE0001 PTOTEAOE0013 PTOTEMOE0001 DE0004109103
1999-12-31       0.0875     0.048125      0.10625      0.07125
2000-01-03       0.0875     0.048125      0.10625      0.07125
2000-01-04       0.0875     0.048125      0.10625      0.07125
2000-01-05       0.0875     0.048125      0.10625      0.07125
2000-01-06       0.0875     0.048125      0.10625      0.07125
2000-01-07       0.0875     0.048125      0.10625      0.07125