@mhenderson,
Can you help me correct the following in such a way that the function returns "NA" when there is an error , else returns a value ? Based on your input , I understand that the function needs to be re-written and I tried it below. Currently it returns just "NA"
# Reprex
# Attach desired packages
suppressPackageStartupMessages(library(tvm))
# Provide input data
CF <- c(-78662, -32491, -32492, 7651, 40300)
d <- as.Date(c("2019-06-30", "2019-09-30", "2019-12-31", "2020-03-31", "2020-06-30"))
# Call the XIRR Function
Res1 <- vector()
Res1 <- sapply(1:length(d), function(i) {
tryCatch({
expr = xirr(i, CF[1:i],
d[1:i])
}, error = function(i) { return(NA) })
})
In the above code, by changing the inputs , I am ensuring that the code should return a value starting at i = 4 , because at i = 4, I would expect to see a value since there is a sign change I expect to have a value. Similarly at i =5.