Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

How to create a summation function with data frame in R?

$
0
0

Just for fun, I am trying to create a basic savings calculator. My current code is:

value <- function(years,apr,initial,investment) {
df <- as.data.frame(matrix(nrow = years, ncol = 2))
colnames(df) <- c("year","value")
df$years <- c(1:years)
  for (i in 1:years) {
    current_value <-(last_value+investment)*apr 
  }
#repeating calculation for the data frame
print(df)

What I am trying to do is have the calculator create a table that displays the value each year. I've adapted my code from an old homework assignment, so I am not concerned with how to make the data frame. However, I do not know how to make the formula for the summation.

I am trying to model

Current Value = (Cumulative Value + Investment)*(Annual Percentage Rate)

As an example, let's say initial value is 10, investment is 10, and the APR is 1.05

(10+10)*(1.05)=21

(21+10)*(1.05)=32.55

(32.55+10)*(1.05)=44.68

and so on.

Year is there to number the rows accordingly.


Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>