Warning: session_start(): open(/tmp/sess_d61585ab2b9a3e2cc45443c769f5e191, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
cumsum() Function In R: Sum of a Numeric Object's Cumulative Value - LearnShareIT

cumsum() Function In R: Sum of a Numeric Object’s Cumulative Value

cumsum r

It is highly typical in many data analytics to compute the cumulative sum of your variables of interest.

The cumsum() function in the R programming language makes it simple to compute the cumulative sum.

What is the cumsum() function in R?

The cumulative sum is the sum of a collection of numbers whose sum value increases as the sequence of numbers increases.

The cumsum() function is used to compute the cumulative sum of the vector supplied as an input. We can read the syntax of this function below.

Syntax:

cumsum(x)

Parameters:

  • x: The numeric

How to use the cumsum() function in R?

Using the cumsum() function with a vector

In this example, we will use the cumsum() function in R with a vector. So, we need to create a vector as follows:

# Create the vector
x <- 10:15
y <- -10:-5

# View the vectors
x
y

Output

[1] 10 11 12 13 14 15
[1] -10  -9  -8  -7  -6  -5

Now, we will apply the cumsum() function to these vectors.

# Create the vector
x <- 10:15
y <- -10:-5

# Using the cumsum() function
cat("Using the cumsum() function for vector x:", cumsum(x), "\n")
cat("Using the cumsum() function for vector y:", cumsum(y))

Output

Using the cumsum() function for vector x: 10 21 33 46 60 75
Using the cumsum() function for vector y: -10 -19 -27 -34 -40 -45

For another example, we will apply this function with a float number.

# Create the vector
x <- c(2.2, 6.3, 4.6, 2.3)
y <- c(2.1, 2.6, 3.5, 9.1)

# Using the cumsum() function
cat("Using the cumsum() function for vector x:", cumsum(x), "\n")
cat("Using the cumsum() function for vector y:", cumsum(y))

Output

Using the cumsum() function for vector x: 2.2 8.5 13.1 15.4
Using the cumsum() function for vector y: 2.1 4.7 8.2 17.3

Using the cumsum() function to plot 

We can use the plot() function to visualize the cumsum() function’s graph. The output of the cumulative sum can be neatly visualized using the plot() function. Let’s use the vector below as our data.

# Create the data
data <- c(10, 12, 16, 18, 20, 25)

The cumsum() function in R can be used to determine the vector’s cumulative sum.

# Create the data
data <- c(10, 12, 16, 18, 20, 25)

# Using cumsum() function
res <- cumsum(data)
res

Output

[1]  10  22  38  56  76 101

Now we will use the results below to plot the graph.

# Create the data
data <- c(10, 12, 16, 18, 20, 25)

# Using cumsum() function
res <- cumsum(data)

# Plot
plot(
    x = seq_len(length(res)),
    y = res, col = "red",
    xlab = "Size of the Example Vector", ylab = "Cumulative Sum",
    main = "Cumulative Analysis"
)

# Add background
rect(0, 55, 10, 0, col = "grey92", border = "black", )

# Add line
points(
    x = seq_len(length(res)), y = res,
    type = "l", col = "#1b98e0"
)

# Add points
points(
    x = seq_len(length(res)), y = res,
    pch = 16, col = "#1b98e0"
)

Output

Summary

This article demonstrates using the cumsum() function in the R programming language. If you have any questions, please leave a comment below.

Have a great day!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *