简体   繁体   English

如何同时在多个 csv 上运行一个函数

[英]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:让我们分步进行:

  1. You need to read in multiple csv-files at once, an answer to this can be easily found here:您需要一次读取多个 csv 文件,可以在此处轻松找到答案:

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!请注意原帖中对此功能的条件和评论!

  1. Next we want to again "apply" your defined function on all data frames in the list.接下来,我们要再次在列表中的所有数据框上“应用”您定义的函数。 So the solution can be found here:所以可以在这里找到解决方案:

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.

相关问题 如何在组中同时应用创建csv文件的功能? - How to apply a function of creating csv file on groups at same time? 如何在 R 中使用 for 循环为多个 .csv 文件运行相同的代码? - How do I run the same code for multiple .csv files with a for loop in R? 同时将 function 传递给多个数据帧 - Passing a function to multiple dataframes at the same time 导入多个 CSV 文件,运行一个函数,然后将结果合并到 R 中的数据框中 - Import Multiple CSV Files, Run A Function, Then Combine Results Into A Dataframe In R 如何在php服务器中同时运行工具 - How to run tools in php server at same time 如何使用 R 同时计算多个子量表的 Cronbach's alpha? - How to compute Cronbach's alpha for multiple subscales at the same time using R? R Shiny我如何不能多次运行同一功能? - R shiny How do I not run thw same function multiple times? 我将如何使用 purrr 根据场景列表和模拟功能多次运行模拟 - How would I use purrr to run a simulation multiple time based upon a list of scenarios and a simulation function 同时导入多个csv文件希望在R中将日期数据转换成(日期和时间)格式 - Importing multiple csv files at the same time want the date data converted into (date and time) format in R 如何同时使用“for loop”和“write.csv”? - how to use “for loop” and “write.csv” at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM