• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to Compute Compound Interest with Tidyverse

rstats101 · September 27, 2024 ·

In this post, we will learn how to calculate compound interest in R using tidyverse framework. Getting an intuition behind how compound interest can influence investing returns is wxtremely useful in personal final. On a high level, compounding interest refers to the process of earning interest not only on the the original investment amount (i.e. principal amount of money), but also on the interest that accumulates over time. This results in a “compounding” effect, where the investment or debt grows at an increasing rate.

Here is the formula for computing compound interest and it shows the variables that influence the compounding.

Compound Interest Formula
Compound Interest Formula

Let us get started computing compound interest on principal amount of $1000 over 10 years period with interest rate of 5%.

library(tidyverse)
theme_set(theme_bw(16))
# Starting amount
principal <- 1000    
# Time period in years
years <- 10          
# Interest rate (5% in this case)
rate <- 0.05          
tibble(year = 0:years)

# A tibble: 11 × 1
    year
   <int>
 1     0
 2     1
 3     2
 4     3
 5     4
 6     5
 7     6
 8     7
 9     8
10     9
11    10

By applying the compound interest formula we can compute compound interest for each year.

tibble(year = 0:years) |>
  mutate(amount = principal * (1 + rate) ^ year)

# A tibble: 11 × 2
    year amount
   <int>  <dbl>
 1     0  1000 
 2     1  1050 
 3     2  1102.
 4     3  1158.
 5     4  1216.
 6     5  1276.
 7     6  1340.
 8     7  1407.
 9     8  1477.
10     9  1551.
11    10  1629.

We can also compute the gain or interest earned due to compound at each year

tibble(year = 1:years) |>
  mutate(amount = principal * (1 + rate) ^ year,
         interest_earned = amount - principal )

# A tibble: 10 × 3
    year amount interest_earned
   <int>  <dbl>           <dbl>
 1     1  1050              50 
 2     2  1102.            102.
 3     3  1158.            158.
 4     4  1216.            216.
 5     5  1276.            276.
 6     6  1340.            340.
 7     7  1407.            407.
 8     8  1477.            477.
 9     9  1551.            551.
10    10  1629.            629.

Let us visualize the growth of investment return due to compound interest over 10 years.

# Create a dataframe to compute compound interest for each year
compound_interest <- tibble(year = 0:years) |>
  # Calculate accumulated amount each year
  # Calculate interest earned each year
  mutate( amount = principal * (1 + rate) ^ year,   
          interest_earned = amount - principal
          )
# Display the results
print(compound_interest)

# A tibble: 11 × 3
    year amount interest_earned
   <int>  <dbl>           <dbl>
 1     0  1000               0 
 2     1  1050              50 
 3     2  1102.            102.
 4     3  1158.            158.
 5     4  1216.            216.
 6     5  1276.            276.
 7     6  1340.            340.
 8     7  1407.            407.
 9     8  1477.            477.
10     9  1551.            551.
11    10  1629.            629.

# Visualize the compound interest gained on 1000 over the years
compound_interest |>
  ggplot(aes(x = year, y = amount)) +
  geom_line(color = "blue", linewidth = 1) +
  geom_point(color = "red", size = 2) +
  labs(
    title = paste("Compound Interest Over", years),
    x = "Year",
    y = "Final Amount",
    subtitle = paste("Interest Rate:", rate * 100, "%")
  ) +
  scale_x_continuous(breaks=scales::breaks_pretty(n=10))+
  scale_y_continuous(breaks=scales::breaks_pretty(n=8))
ggsave("growth_of_1000_compound_interest_example.png")

Computing Compound Interest Example
Computing Compound Interest Example

Related

Filed Under: Compound Interest, rstats101 Tagged With: Calculate Compound Interest

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version