'Transform code of two loops ("For") with lapply or sapply
I am learning how to code in R and I have written a piece of code that calculate the Net Present Value (NPV) of various assets at different points in time, given a data frame. For this, I am using two "for" loops and I find it very slow.
The first loop get the asset for which I need to calculate the NPV. The second loop get the Date from which the NPV will be calculated with the sub-function "npv_date"
I was wondering if I can get some assistance to convert it to use either the sapply or lapply or another function. Here is my working R code:
#Create the column NPV in the data frame
Ejemplo <- as.data.frame(Ejemplo %>% mutate(npv = -1))
for(j in unique(Ejemplo$Key)){
#Create subset per Asset by the Key
SubTable <- subset(Ejemplo, Ejemplo$Key==j)
for (i in unique(SubTable$KeyDate)){
#cal the function that calculate the NPV per date
Ejemplo <- npv_date(Tabla=Ejemplo, key=j, keyDate=i)
}
}
#function that calculate the npv per asset and date
npv_date <- function(Tabla, key, keyDate){
#create a subset only with the asset selected
subTable <- subset(Tabla, Tabla$Key==key)
#ger the flow that we need to calculate the NPV
flow <- unique(subTable [match(keyDate,subTable$KeyDate):length(subTable$Key), "Flow"]
#get the rate for the date on which we are going to calculate the NPV
#Question: i am using only one Rate, the rate corresponding to the month
#of calculation, but a vector of rates can be used
irr <- (subTable %>% filter(KeyDate == keyDate))$Rate
#calculate the NPV calling the sub-function dcf
npv <- sum(dcf(x=flow, r=irr))
#Assign the value to the corresponding row
Tabla[["npv"]][(Tabla[["KeyDate"]]==keyDate)&(Tabla[["Key"]]==key)] <- npv
return(Tabla)
}
dcf <- function(x, r, t0=FALSE){
# calculates discounted cash flows (DCF) given cash flow and discount rate
# x - cash flows vector
# r - vector or numeric value of discount rate(s), in decimals. Single values will be recycled
# t0 - cash flow starts in year 0, default is FALSE, i.e. discount rate in first period is zero. Will be ignored if r is a vector
if(length(r)==1){
r <- rep(r, length(x))
if(t0==TRUE){r[1]<-0}
}
x/cumprod(1+r)
}
I attach an example with twoo asset, where you can find the Date, Flow (Cah flow generated by the Asset A and B), Key (ID Asset), KeyDate (ID Asset + Date), Rate (cash flow discount rate to calculate NPV) columns and the npv column with the result of the Net Present Value executing the code with the two for
Ejemplo <- structure(list(Date = structure(c(24136, 24165, 24196, 24226,
24257, 24287, 24318, 24349, 24379, 24410, 24440, 24471, 24136,
24165, 24196, 24226, 24257, 24287, 24318, 24349, 24379, 24410,
24440, 24471, 24502, 24530, 24561, 24591, 24622, 24652, 24683,
24714, 24744, 24775, 24805, 24836, 24867, 24895, 24926, 24956,
24987, 25017, 25048, 25079, 25109, 25140, 25170, 25201, 25232,
25260, 25291, 25321, 25352, 25382, 25413, 25444, 25474, 25505,
25535, 25566, 25597, 25626, 25657, 25687, 25718, 25748, 25779,
25810, 25840, 25871, 25901, 25932, 25963, 25991, 26022, 26052,
26083, 26113, 26144, 26175, 26205, 26236, 26266, 26297, 26328,
26356, 26387, 26417, 26448, 26478, 26509, 26540, 26570, 26601,
26631, 26662, 26693, 26721, 26752, 26782, 26813, 26843, 26874,
26905, 26935, 26966, 26996, 27027, 27058, 27087, 27118, 27148,
27179, 27209, 27240, 27271, 27301, 27332, 27362, 27393, 27424,
27452, 27483, 27513, 27544, 27574, 27605, 27636, 27666, 27697,
27727, 27758, 27789, 27817, 27848, 27878, 27909, 27939, 27970,
28001, 28031, 28062, 28092, 28123, 28154, 28182, 28213, 28243,
28274, 28304, 28335, 28366, 28396, 28427, 28457, 28488, 28519,
28548, 28579, 28609, 28640, 28670, 28701, 28732, 28762, 28793,
28823, 28854, 28885, 28913, 28944, 28974, 29005, 29035, 29066,
29097, 29127, 29158, 29188, 29219, 29250, 29278, 29309, 29339,
29370, 29400, 29431, 29462, 29492, 29523, 29553, 29584, 29615
), class = "Date"), Flow = c(626.61225, 626.61225, 626.61225,
626.61225, 626.61225, 626.61225, 626.61225, 626.61225, 626.61225,
626.61225, 626.61225, 147514.77225, 1172.37036865556, 1096.73357067778,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1096.73357067778,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1096.73357067778,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1096.73357067778,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 1172.37036865556, 1058.91517168889,
1172.37036865556, 1134.55196966667, 1172.37036865556, 1134.55196966667,
1172.37036865556, 1172.37036865556, 1134.55196966667, 1172.37036865556,
1134.55196966667, 1172.37036865556, 618216.771575733), Key = c("200015B",
"200015B", "200015B", "200015B", "200015B", "200015B", "200015B",
"200015B", "200015B", "200015B", "200015B", "200015B", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A",
"200015A", "200015A", "200015A", "200015A", "200015A", "200015A"
), KeyDate = c("200015B2036-01-31", "200015B2036-02-29", "200015B2036-03-31",
"200015B2036-04-30", "200015B2036-05-31", "200015B2036-06-30",
"200015B2036-07-31", "200015B2036-08-31", "200015B2036-09-30",
"200015B2036-10-31", "200015B2036-11-30", "200015B2036-12-31",
"200015A2036-01-31", "200015A2036-02-29", "200015A2036-03-31",
"200015A2036-04-30", "200015A2036-05-31", "200015A2036-06-30",
"200015A2036-07-31", "200015A2036-08-31", "200015A2036-09-30",
"200015A2036-10-31", "200015A2036-11-30", "200015A2036-12-31",
"200015A2037-01-31", "200015A2037-02-28", "200015A2037-03-31",
"200015A2037-04-30", "200015A2037-05-31", "200015A2037-06-30",
"200015A2037-07-31", "200015A2037-08-31", "200015A2037-09-30",
"200015A2037-10-31", "200015A2037-11-30", "200015A2037-12-31",
"200015A2038-01-31", "200015A2038-02-28", "200015A2038-03-31",
"200015A2038-04-30", "200015A2038-05-31", "200015A2038-06-30",
"200015A2038-07-31", "200015A2038-08-31", "200015A2038-09-30",
"200015A2038-10-31", "200015A2038-11-30", "200015A2038-12-31",
"200015A2039-01-31", "200015A2039-02-28", "200015A2039-03-31",
"200015A2039-04-30", "200015A2039-05-31", "200015A2039-06-30",
"200015A2039-07-31", "200015A2039-08-31", "200015A2039-09-30",
"200015A2039-10-31", "200015A2039-11-30", "200015A2039-12-31",
"200015A2040-01-31", "200015A2040-02-29", "200015A2040-03-31",
"200015A2040-04-30", "200015A2040-05-31", "200015A2040-06-30",
"200015A2040-07-31", "200015A2040-08-31", "200015A2040-09-30",
"200015A2040-10-31", "200015A2040-11-30", "200015A2040-12-31",
"200015A2041-01-31", "200015A2041-02-28", "200015A2041-03-31",
"200015A2041-04-30", "200015A2041-05-31", "200015A2041-06-30",
"200015A2041-07-31", "200015A2041-08-31", "200015A2041-09-30",
"200015A2041-10-31", "200015A2041-11-30", "200015A2041-12-31",
"200015A2042-01-31", "200015A2042-02-28", "200015A2042-03-31",
"200015A2042-04-30", "200015A2042-05-31", "200015A2042-06-30",
"200015A2042-07-31", "200015A2042-08-31", "200015A2042-09-30",
"200015A2042-10-31", "200015A2042-11-30", "200015A2042-12-31",
"200015A2043-01-31", "200015A2043-02-28", "200015A2043-03-31",
"200015A2043-04-30", "200015A2043-05-31", "200015A2043-06-30",
"200015A2043-07-31", "200015A2043-08-31", "200015A2043-09-30",
"200015A2043-10-31", "200015A2043-11-30", "200015A2043-12-31",
"200015A2044-01-31", "200015A2044-02-29", "200015A2044-03-31",
"200015A2044-04-30", "200015A2044-05-31", "200015A2044-06-30",
"200015A2044-07-31", "200015A2044-08-31", "200015A2044-09-30",
"200015A2044-10-31", "200015A2044-11-30", "200015A2044-12-31",
"200015A2045-01-31", "200015A2045-02-28", "200015A2045-03-31",
"200015A2045-04-30", "200015A2045-05-31", "200015A2045-06-30",
"200015A2045-07-31", "200015A2045-08-31", "200015A2045-09-30",
"200015A2045-10-31", "200015A2045-11-30", "200015A2045-12-31",
"200015A2046-01-31", "200015A2046-02-28", "200015A2046-03-31",
"200015A2046-04-30", "200015A2046-05-31", "200015A2046-06-30",
"200015A2046-07-31", "200015A2046-08-31", "200015A2046-09-30",
"200015A2046-10-31", "200015A2046-11-30", "200015A2046-12-31",
"200015A2047-01-31", "200015A2047-02-28", "200015A2047-03-31",
"200015A2047-04-30", "200015A2047-05-31", "200015A2047-06-30",
"200015A2047-07-31", "200015A2047-08-31", "200015A2047-09-30",
"200015A2047-10-31", "200015A2047-11-30", "200015A2047-12-31",
"200015A2048-01-31", "200015A2048-02-29", "200015A2048-03-31",
"200015A2048-04-30", "200015A2048-05-31", "200015A2048-06-30",
"200015A2048-07-31", "200015A2048-08-31", "200015A2048-09-30",
"200015A2048-10-31", "200015A2048-11-30", "200015A2048-12-31",
"200015A2049-01-31", "200015A2049-02-28", "200015A2049-03-31",
"200015A2049-04-30", "200015A2049-05-31", "200015A2049-06-30",
"200015A2049-07-31", "200015A2049-08-31", "200015A2049-09-30",
"200015A2049-10-31", "200015A2049-11-30", "200015A2049-12-31",
"200015A2050-01-31", "200015A2050-02-28", "200015A2050-03-31",
"200015A2050-04-30", "200015A2050-05-31", "200015A2050-06-30",
"200015A2050-07-31", "200015A2050-08-31", "200015A2050-09-30",
"200015A2050-10-31", "200015A2050-11-30", "200015A2050-12-31",
"200015A2051-01-31"), Rate = c(0.00145610800020436, 0.00145610800020436,
0.00145610800020436, 0.00145610800020436, 0.00145610800020436,
0.00145610800020436, 0.00145610800020436, 0.00145610800020436,
0.00145610800020436, 0.00145610800020436, 0.00145610800020436,
0.00145610800020436, 0.00189991473897737, 0.00177733959452722,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00177733959452722,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00177733959452722,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00177733959452722,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00189991473897737, 0.00171605202230214,
0.00189991473897737, 0.00183862716675229, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00189991473897737,
0.00183862716675229, 0.00189991473897737, 0.00183862716675229,
0.00189991473897737, 0.00147090173340183), npv = c(151794.327418487,
151388.744103027, 150982.570214456, 150575.804892837, 150168.447276979,
149760.496504437, 149351.951711512, 148942.812033245, 148533.076603419,
148122.744554557, 147711.815017915, 147300.287123487, 613813.30968176,
625416.216640273, 613876.579913955, 619590.650363644, 613902.27239164,
619562.128968382, 613928.0625891, 613922.103195229, 619500.223356209,
613947.968817945, 619471.36913215, 613973.932818982, 613968.060574637,
630464.071341746, 614069.737791426, 619455.547720436, 614096.164933345,
619426.529061673, 614122.69258929, 614117.102975826, 619363.874120433,
614143.710268341, 619334.51804462, 614170.418760075, 614164.919822266,
629636.556056425, 614267.345781225, 619317.433907436, 614294.52451311,
619287.906902155, 614321.806617647, 614316.59530386, 619224.485930791,
614343.961353307, 619194.616817945, 614371.431487858, 614366.314457093,
628791.838301082, 614469.506408292, 619176.241811647, 614497.45404582,
619146.195129813, 614525.507980488, 614520.683681922, 619081.991055034,
614548.825969097, 619051.597470577, 614577.075293749, 614572.348968682,
623440.759857194, 614638.50616062, 618994.014891329, 614667.096578424,
618963.297498735, 614695.795738142, 614691.294971797, 618898.082726276,
614720.086168632, 618867.012242409, 614748.986871033, 614744.587163305,
627010.819082074, 614849.217851725, 618845.61442753, 614878.609698625,
618814.350827013, 614908.113335626, 614904.015954614, 618748.312970872,
614933.616222764, 618716.691239219, 614963.329073733, 614959.336597915,
626111.517245459, 615064.784072744, 618693.906411911, 615094.995812649,
618662.084429579, 615125.322461069, 615121.637758875, 618595.205144855,
615152.065738316, 618563.019879196, 615182.609448726, 615179.033586924,
625193.520632855, 615285.316665883, 618538.817125705, 615316.367188087,
618506.424316336, 615347.535809064, 615344.273293286, 618438.684849255,
615375.548053435, 618405.923489536, 615406.941765232, 615403.792115705,
621289.537476003, 615473.111651064, 618342.383274176, 615504.876440068,
618309.267463521, 615536.762044514, 615533.85904245, 618240.438501881,
615565.854880461, 618206.947469741, 615597.972412695, 615595.185705111,
623261.342047719, 615703.05159333, 618179.459234866, 615735.690944913,
618145.743760308, 615768.454438285, 615765.991632014, 618076.011161883,
615798.870372205, 618041.914932739, 615831.874164684, 615829.531850486,
622284.095709742, 615938.28906001, 618012.903942059, 615971.823122985,
617978.575438281, 616005.484730728, 616003.472261803, 617907.919062799,
616037.25424512, 617873.204148013, 616071.164716155, 616069.277033603,
621286.534277733, 616178.946099343, 617842.636462324, 616213.395486724,
617807.68126612, 616247.975900254, 616246.424143876, 617736.080524425,
616281.130179894, 617700.733134719, 616315.968218208, 616314.545641437,
618946.910812147, 616387.329172415, 617630.686125455, 616422.571132135,
617594.950817448, 616457.947132504, 616456.794303566, 617522.174521046,
616492.300469685, 617486.039821442, 616527.941681348, 616526.921836084,
619190.229666114, 616638.331525985, 617451.89976914, 616674.528157884,
617415.506413833, 616710.862461471, 616710.190150093, 617341.738502388,
616746.660091897, 617304.93968368, 616783.268744906, 616782.733999293,
618128.352639199, 616895.116655191, 617269.128619764, 616932.289953705,
617232.062550589, 616969.604638599, 616969.423915297, 617157.280922968,
617006.879836953, 617119.803183259, 617044.478219929, 617308.771034375
)), row.names = c(169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L,
177L, 178L, 179L, 180L, 780832L, 780833L, 780834L, 780835L, 780836L,
780837L, 780838L, 780839L, 780840L, 780841L, 780842L, 780843L,
780844L, 780845L, 780846L, 780847L, 780848L, 780849L, 780850L,
780851L, 780852L, 780853L, 780854L, 780855L, 780856L, 780857L,
780858L, 780859L, 780860L, 780861L, 780862L, 780863L, 780864L,
780865L, 780866L, 780867L, 780868L, 780869L, 780870L, 780871L,
780872L, 780873L, 780874L, 780875L, 780876L, 780877L, 780878L,
780879L, 780880L, 780881L, 780882L, 780883L, 780884L, 780885L,
780886L, 780887L, 780888L, 780889L, 780890L, 780891L, 780892L,
780893L, 780894L, 780895L, 780896L, 780897L, 780898L, 780899L,
780900L, 780901L, 780902L, 780903L, 780904L, 780905L, 780906L,
780907L, 780908L, 780909L, 780910L, 780911L, 780912L, 780913L,
780914L, 780915L, 780916L, 780917L, 780918L, 780919L, 780920L,
780921L, 780922L, 780923L, 780924L, 780925L, 780926L, 780927L,
780928L, 780929L, 780930L, 780931L, 780932L, 780933L, 780934L,
780935L, 780936L, 780937L, 780938L, 780939L, 780940L, 780941L,
780942L, 780943L, 780944L, 780945L, 780946L, 780947L, 780948L,
780949L, 780950L, 780951L, 780952L, 780953L, 780954L, 780955L,
780956L, 780957L, 780958L, 780959L, 780960L, 780961L, 780962L,
780963L, 780964L, 780965L, 780966L, 780967L, 780968L, 780969L,
780970L, 780971L, 780972L, 780973L, 780974L, 780975L, 780976L,
780977L, 780978L, 780979L, 780980L, 780981L, 780982L, 780983L,
780984L, 780985L, 780986L, 780987L, 780988L, 780989L, 780990L,
780991L, 780992L, 780993L, 780994L, 780995L, 780996L, 780997L,
780998L, 780999L, 781000L, 781001L, 781002L, 781003L, 781004L,
781005L, 781006L, 781007L, 781008L, 781009L, 781010L, 781011L,
781012L), class = "data.frame")
Here you have a web with an explanation about the Net present value:
Solution 1:[1]
This is still complicated. So far, I think this is the way it is supposed to be
df_Ejemplo %>%
group_by(Key) %>%
mutate(
npv = sum(dcf(x=Flow, Rate))
) ->
df_Ejemplo
> all.equal(df_Ejemplo$npv, Ejemplo$npv)
[1] "Mean relative difference: 0.003511715"
But it disagrees with the result.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mossa |