R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
The cumsum()
function in R computes the cumulative sum of elements in a numeric object. This is useful for a variety of applications, including running totals or understanding the growth of a value over time.
Let's go over the basics of the cumsum()
function with some examples:
The most straightforward use of cumsum()
is to calculate the cumulative sum of a numeric vector:
numbers <- c(1, 2, 3, 4, 5) cumulative_sum <- cumsum(numbers) print(cumulative_sum) # Output: 1 3 6 10 15
In this example, the first value is 1
(the sum of just the first element), the second is 1+2=3
, the third is 1+2+3=6
, and so on.
The cumsum()
function handles negative values as expected:
numbers <- c(5, -3, 7, -2, 4) cumulative_sum <- cumsum(numbers) print(cumulative_sum) # Output: 5 2 9 7 11
If there are any NA
values in the numeric object, the cumulative sum will return NA
for that value and all subsequent values:
numbers <- c(1, 2, NA, 4, 5) cumulative_sum <- cumsum(numbers) print(cumulative_sum) # Output: 1 3 NA NA NA
You can use cumsum()
with matrices to get the cumulative sum of each column or row:
mat <- matrix(1:9, ncol=3) print(mat) # 1 4 7 # 2 5 8 # 3 6 9 # Cumulative sum of columns col_cumsum <- apply(mat, 2, cumsum) print(col_cumsum) # Cumulative sum of rows row_cumsum <- t(apply(mat, 1, cumsum)) print(row_cumsum)
To compute the cumulative sum for specific columns in a data frame, use cumsum()
directly on the desired column:
df <- data.frame(A = c(1, 2, 3, 4), B = c(5, 6, 7, 8)) df$A_cumsum <- cumsum(df$A) df$B_cumsum <- cumsum(df$B) print(df)
The cumsum()
function in R is a simple yet powerful tool for calculating the running total of numeric objects. Whether you're working with vectors, matrices, or data frames, cumsum()
provides a straightforward way to track the accumulation of values.
R cumsum() Function Example:
# Basic cumsum() example x <- c(1, 2, 3, 4, 5) cumulative_sum <- cumsum(x)
How to Use cumsum() to Calculate Cumulative Sum in R:
# Calculate cumulative sum in R x <- c(1, 2, 3, 4, 5) cumulative_sum <- cumsum(x)
Cumulative Sum by Groups in R Using cumsum():
# Cumulative sum by groups data <- data.frame(group = rep(1:2, each = 5), value = 1:10) data$cumulative_sum <- ave(data$value, data$group, FUN = cumsum)
Rolling Cumulative Sum in Time Series Data with cumsum() in R:
# Rolling cumulative sum set.seed(123) time_series <- rnorm(10) rolling_cumsum <- cumsum(time_series)
Visualizing Cumulative Sum with ggplot2 in R:
# Visualizing cumulative sum with ggplot2 library(ggplot2) set.seed(123) data <- data.frame(value = rnorm(10)) data$cumulative_sum <- cumsum(data$value) ggplot(data, aes(x = seq_along(value), y = cumulative_sum)) + geom_line() + labs(title = "Cumulative Sum with ggplot2", x = "Index", y = "Cumulative Sum")
Cumulative Sum with Conditional Statements Using cumsum() in R:
# Cumulative sum with conditional statements x <- c(1, 2, 3, 4, 5) conditional_cumsum <- cumsum(ifelse(x > 2, x, 0))
Resetting Cumulative Sum at Specific Points in R:
# Resetting cumulative sum at specific points x <- c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5) reset_indices <- c(5, 10) reset_cumsum <- cumsum(x * !duplicated(cumsum(x > 2) * cumsum(x > 2) %in% reset_indices))
Cumulative Sum vs Running Sum in R:
# Cumulative sum vs running sum x <- c(1, 2, 3, 4, 5) cumulative_sum <- cumsum(x) running_sum <- cumsum(x)
Calculating Moving Average with cumsum() in R:
# Calculating moving average set.seed(123) time_series <- rnorm(10) window_size <- 3 moving_average <- cumsum(time_series) / seq_along(time_series)