I should mention that even after the accepted answer your functions seem to contain a small error:
npv<-function(i,cf,t=seq(along=cf)) sum (cf/(1+i)^t)
irr <- function(cf) {
if(all(cf < 0)) return(0)
uniroot(npv, c(0,1), cf=cf)$root
}
Which considering this example:
> npv(c(-123400, 36200, 54800, 48100), i = 0.025)
[1] 8528.911
The problem is that the defaults for t=seq(along=cf)
will make you discount the cash flows by 1:4
. Since the initial cash outflow of -123400
is usually considered a PV, you end up out of sync for the discounting.
This should fix things:
npv<-function(i,cf,t=seq(along=cf)-1) sum (cf/(1+i)^t)
Giving you this:
> npv(c(-123400, 36200, 54800, 48100), i = 0.025)
[1] 8742.134
But generally I would use financial
or FinCal
for computing IRR or NPV (or MIRR):
> require(financial)
> cf(c(-123400, 36200, 54800, 48100), i = 2.5)
Cash Flow Model
Flows:
1 2 3 4
-123400 36200 54800 48100
IRR%: 5.96
NPV Extremes at I%:
I% NPV NFV NUS
1 2.5 8742.13 9414.32 3060.95
> require(FinCal)
> npv(c(-123400, 36200, 54800, 48100), r = 0.025)
[1] 8742.134
> irr(c(-123400, 36200, 54800, 48100))
[1] 0.05959787