R Markdown

This task involves 3 functions. The three functions taken are: Calculating the compound interest, generating summary statistics from the given set of numbers, and third one is to generate a histogram.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Function 1: Calculation of Compound Interest
# principal: The initial amount of money.
# rate: The annual interest rate (in percentage).
# time: The time the money is invested for (in years).
# comp_per_year: Number of times interest is compounded per year. Default is 1 (simple interest).

calculate_compound_interest <- function(principal, rate, time, comp_per_year = 1) {
  final_amount <- principal * (1 + (rate / 100) / comp_per_year)^(comp_per_year * time)
  compound_interest <- final_amount - principal
  return(compound_interest)
}

# Function 2: Generation of Summary Statistics
# This function computes basic summary statistics (mean, median, min, max, SD) for a numeric vector.
# data_vector: The input data for which summary statistics are computed.
# It gives a list containing the mean, median, minimum, maximum, and standard deviation.
generate_summary_statistics <- function(data_vector) {
  if (!is.numeric(data_vector)) stop("Input must be a numeric vector.")
  summary_stats <- list(
    mean = mean(data_vector, na.rm = TRUE),
    median = median(data_vector, na.rm = TRUE),
    minimum = min(data_vector, na.rm = TRUE),
    maximum = max(data_vector, na.rm = TRUE),
    standard_deviation = sd(data_vector, na.rm = TRUE)
  )
  return(summary_stats)
}

# Function 3: Plotting a Histogram
# This function creates a customized histogram for a numeric vector.
# data_vector: The data to be plotted.
# bins: Number of bins for the histogram.
# color: Color of the histogram bars.
# title: Title of the histogram.
plot_histogram <- function(data_vector, bins = 10, color = "blue", title = "Histogram") {
  if (!is.numeric(data_vector)) stop("Input must be a numeric vector.")
  library(ggplot2)
  plot <- ggplot(data.frame(x = data_vector), aes(x = x)) +
    geom_histogram(bins = bins, fill = color, color = "black", alpha = 0.7) +
    labs(title = title, x = "Values", y = "Frequency") +
    theme_minimal()
  return(plot)
}

# For Compound Interest
ci <- calculate_compound_interest(principal = 1000, rate = 5, time = 2, comp_per_year = 4)
print(ci)
## [1] 104.4861
# for computing Summary Statistics
stats <- generate_summary_statistics(c(10, 20, 30, 40, 50))
print(stats)
## $mean
## [1] 30
## 
## $median
## [1] 30
## 
## $minimum
## [1] 10
## 
## $maximum
## [1] 50
## 
## $standard_deviation
## [1] 15.81139
# For plotting the Histogram
hist_plot <- plot_histogram(c(10, 20, 20, 30, 30, 30, 40, 50), bins = 5, color = "green", title = "Histogram by Yatharth")
## Warning: package 'ggplot2' was built under R version 4.4.2
print(hist_plot)

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.