[英]How to run a function on multiple csv's at the same time
I have a set of 3 csv's which are all saved in the below directory:我有一组 3 个 csv,它们都保存在以下目录中:
setwd("~/R/CashFlows")
The csv's are named: csv 的名称为:
"Cashflows1.csv" "Cashflows2.csv" "Cashflows3.csv"
And are all in a form similar to the below (the below example is "Cashflows1.csv")并且都是类似于下面的形式(下面的例子是“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)我创建了以下函数来返回每个现金流文件的年化 IRR(示例中使用了 Cashflows1.csv)
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.我将如何更改函数,以便它同时计算目录中所有 csv 的 IRR,因为我目前必须为“dat”变量规定特定的现金流文件。 But would like it to calculate it for an "X" number of csv's.
但是希望它为“X”个csv计算它。
Can anyone help me?谁能帮我?
We have multiple things to tackle here but all are solvable with similar tools, the apply
-family of functions.我们在这里有很多事情要解决,但所有事情都可以用类似的工具来解决,
apply
函数系列。
Let's go at it in steps:让我们分步进行:
How to import multiple .csv files at once? 如何一次导入多个 .csv 文件?
from this we can take @A5C1D2H2I1M1N2O1R2T1 excellent code to create a list of data frames with all csvs accounted for:由此我们可以采用@A5C1D2H2I1M1N2O1R2T1 优秀代码来创建一个包含所有 csvs 的数据帧列表:
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 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:使用
lapply
我们可以接收一个数据框列表(在步骤 1 中定义)并将一个函数应用于列表中的每个数据框,如下所示:
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.请注意,您仍然需要提前定义函数
npv
而不是irr
,结果将是输出列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.