I'm trying to calculate IRR by group using the package "jrvFinance" - function "irr" but i don't know how.
I have this for only 1 group:
example:
pr1 <- data.frame(idC=1,period = 0:12,
cf = c(-10000,1623.8,1630.47,1637.88,1646.09,1655.21,1665.32,1676.54,1688.99,1702.81,1718.14,1735.15,1753.97))
irr1 <- pr1 %>%
select(cf) %>% .[[1]] %>% irr()
pr1<-pr1 %>%mutate(calculate=irr1)
But i have a data.frame with several groups (idC), how can i get the same result by group in the same data.frame? in this example i only use 2 groups (idC column)
pr1 <- data.frame(idC=1,period = 0:12,
cf = c(-10000,1623.8,1630.47,1637.88,1646.09,1655.21,1665.32,1676.54,1688.99,1702.81,1718.14,1735.15,1753.97))
pr2<-data.frame(idC=2,period = 0:12,
cf = c(-10000,1555.79,1562.19,1569.22,1576.93,1585.40,1594.7,1604.91,1616.12,1628.43,1641.94,1656.79,1673.02))
full_pr=rbind(pr1,pr2)
result I need for full_pr:
idC | period | cf | calculate |
---|---|---|---|
1 | 0 | -10000 | 0.1263736 |
1 | 1 | 1623.8 | 0.1263736 |
1 | 2 | 1630.47 | 0.1263736 |
1 | 3 | 1637.88 | 0.1263736 |
1 | 4 | 1646.09 | 0.1263736 |
1 | 5 | 1655.21 | 0.1263736 |
1 | 6 | 1665.32 | 0.1263736 |
1 | 7 | 1676.54 | 0.1263736 |
1 | 8 | 1688.99 | 0.1263736 |
1 | 9 | 1702.81 | 0.1263736 |
1 | 10 | 1718.14 | 0.1263736 |
1 | 11 | 1735.15 | 0.1263736 |
1 | 12 | 1753.97 | 0.1263736 |
2 | 0 | -10000 | 0.1170392 |
2 | 1 | 1555.79 | 0.1170392 |
2 | 2 | 1562.19 | 0.1170392 |
2 | 3 | 1569.22 | 0.1170392 |
2 | 4 | 1576.93 | 0.1170392 |
2 | 5 | 1585.4 | 0.1170392 |
2 | 6 | 1594.7 | 0.1170392 |
2 | 7 | 1604.91 | 0.1170392 |
2 | 8 | 1616.12 | 0.1170392 |
2 | 9 | 1628.43 | 0.1170392 |
2 | 10 | 1641.94 | 0.1170392 |
2 | 11 | 1656.79 | 0.1170392 |
2 | 12 | 1673.02 | 0.1170392 |
If I understand your request correctly, this should work.
The purpose of the as.data.frame()
at the end is because it changes the data frame to a tibble
. You can use print(n = 30)
to see additional rows (it will only show 10 as a tibble
). I just changed it back to a data frame.
fpr2 <- full_pr %>% group_by(idC) %>%
mutate(calculate = irr(cf)) %>% as.data.frame()
# idC period cf calculate
# 1 1 0 -10000.00 0.1263736
# 2 1 1 1623.80 0.1263736
# 3 1 2 1630.47 0.1263736
# 4 1 3 1637.88 0.1263736
# 5 1 4 1646.09 0.1263736
# 6 1 5 1655.21 0.1263736
# 7 1 6 1665.32 0.1263736
# 8 1 7 1676.54 0.1263736
# 9 1 8 1688.99 0.1263736
# 10 1 9 1702.81 0.1263736
# 11 1 10 1718.14 0.1263736
# 12 1 11 1735.15 0.1263736
# 13 1 12 1753.97 0.1263736
# 14 2 0 -10000.00 0.1170393
# 15 2 1 1555.79 0.1170393
# 16 2 2 1562.19 0.1170393
# 17 2 3 1569.22 0.1170393
# 18 2 4 1576.93 0.1170393
# 19 2 5 1585.40 0.1170393
# 20 2 6 1594.70 0.1170393
# 21 2 7 1604.91 0.1170393
# 22 2 8 1616.12 0.1170393
# 23 2 9 1628.43 0.1170393
# 24 2 10 1641.94 0.1170393
# 25 2 11 1656.79 0.1170393
# 26 2 12 1673.02 0.1170393