cumall, cumany & cummean R Functions of dplyr Package (3 Examples)
In this R tutorial you’ll learn how to apply the cumall, cumany, and cummean functions of the dplyr package.
The content looks as follows:
- Example 1: cumall Function – Cumulative Version of all()
- Example 2: cumany Function – Cumulative Version of any()
- Example 3: cummean Function – Cumulative Version of mean()
- Video, Further Resources & Summary
Here’s the step-by-step process.
Example 1: cumall Function – Cumulative Version of all()
Before we can apply the cumall function, we need to install and load the dplyr package to R:
install.packages("dplyr") # Install and load dplyr library("dplyr")
Furthermore, we need to create some example data:
x_logical <- c(TRUE, FALSE, TRUE, TRUE, FALSE) # Create logical vector
Our example data is a logical vector stored in the data object x_logical.
Now, we can use the cumall function as shown below:
cumall(x_logical) # Apply cumall function # TRUE FALSE FALSE FALSE FALSE
As you can see based on the RStudio console output, the cumall function is a cumulative version of the all function.
First, the cumall function checks whether the first element is TRUE; then it checks whether the first AND the second element are TRUE; then it checks whether the first, the second AND the third element are TRUE and so on… At the point where an element is FALSE, the cumall function returns FALSE for the rest of the vector.
Example 2: cumany Function – Cumulative Version of any()
Similar to the cumall function, we can apply the cumany function:
cumany(x_logical) # Apply cumany function # TRUE TRUE TRUE TRUE TRUE
The cumany function is the cumulative version of the any function and tests whether at least one vector element is TRUE.
Example 3: cummean Function – Cumulative Version of mean()
The third function I want to show you is the cummean function. The cummean function is applied to numeric vectors. Let’s create such a vector first:
x_numeric <- c(2, 10, 3, 6) # Create numeric vector
Now, we can apply the cummean command as follows:
cummean(x_numeric) # Apply cummean function # 2.00 6.00 5.00 5.25
As you can see based on the output, the cummean function is the cumulative version of the mean function.
Video, Further Resources & Summary
I have recently published a video instruction on my YouTube channel, which shows the R programming code for other dplyr package functions in RStudio. You can find the video below:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Besides the video, you could have a look at some of the related tutorials on this homepage. You can find a selection of posts about dplyr and other related functions here:
- all & any R Functions
- mean Function in R
- cumsum R Function
- The dplyr Package
- R Functions List (+ Examples)
- The R Programming Language
In summary: At this point you should have learned how to compute the cumulative all, any, mean functions with the dplyr package of the tidyverse in the R programming language. Please tell me about it in the comments section, in case you have further comments and/or questions.
Statistics Globe Newsletter