I have a set of 3 csv's which are all saved in the below directory:
setwd("~/R/CashFlows")
The csv's are named:
"Cashflows1.csv" "Cashflows2.csv" "Cashflows3.csv"
And are all in a form similar to the below (the below example is "Cashflows1.csv")
19/10/2003 -13275
19/11/2003 940.49
19/12/2003 884.71
19/01/2004 832.11
19/02/2004 782.49
19/03/2004 735.74
19/04/2004 691.64
19/05/2004 650.09
19/06/2004 610.91
19/07/2004 573.99
19/08/2004 539.2
19/09/2004 506.42
19/10/2004 475.54
19/11/2004 441.05
19/12/2004 413.91
19/01/2005 388.37
19/02/2005 364.31
19/03/2005 341.66
19/04/2005 320.34
19/05/2005 300.28
19/06/2005 281.39
19/07/2005 263.63
19/08/2005 246.91
19/09/2005 231.2
19/10/2005 216.41
19/11/2005 202.51
19/12/2005 189.43
19/01/2006 177.15
19/02/2006 165.6
19/03/2006 154.75
19/04/2006 144.55
19/05/2006 134.98
19/06/2006 125.99
19/07/2006 117.55
19/08/2006 109.62
19/09/2006 102.18
Or in Vector form:
dat <- read.csv("cashflows1.csv", skip=1, header=F)$V2
> dat
[1] -13275.00 940.49 884.71 832.11 782.49 735.74 691.64 650.09 610.91 573.99 539.20
[12] 506.42 475.54 441.05 413.91 388.37 364.31 341.66 320.34 300.28 281.39 263.63
[23] 246.91 231.20 216.41 202.51 189.43 177.15 165.60 154.75 144.55 134.98 125.99
[34] 117.55 109.62 102.18
I have created the following function to return the annualized IRR of each cashflow file (Cashflows1.csv is used in the example)
setwd("~/R Studio/Technical Test")
> dat <- read.csv("cashflows1.csv", skip=1, header=F)$V2
> npv<-function(i,cf,t=seq(along=cf)) sum (cf/(1+i)^t)
> irr <- function(cf) {uniroot(npv, c(0,1), cf=cf)$root }
> irr(dat)
[1] 0.002384391
> var <- irr(dat)
>
> AIRR <- (1+var)^12-1
> AIRR
[1] 0.02899093
>
How would i go about changing the function such that it calculates the IRR of all the csv's in the directory simultaneously as i currently have to stipulate the specific cashflow file for the "dat" variable. But would like it to calculate it for an "X" number of csv's.
Can anyone help me?
We have multiple things to tackle here but all are solvable with similar tools, the apply
-family of functions.
Let's go at it in steps:
How to import multiple .csv files at once?
from this we can take @A5C1D2H2I1M1N2O1R2T1 excellent code to create a list of data frames with all csvs accounted for:
temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)
Please note the conditions and comments to this functions in the original post!
Same function over multiple data frames in R
Using lapply
we can take in a list of data frames (defined in step 1) and apply one function to each data frame in the list like this:
result <- lapply(myfiles, function(x) {
uniroot(npv, c(0,1), x=x)$root
})
Note that you still need to define the function npv
ahead of time but not irr
, the result will be a list of the outputs.