Open In App

cumall(), cumany() & cummean() R Functions of dplyr Package

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss about cumall(), cummany(), and cummean() functions in R Programming Language. Which are available in dplyr() package.

We have to install and load dplyr package.

Install - install.packages("dplyr")                       
Load - library("dplyr")

cumall()

This function will check whether the first element is TRUE. if yes, then it will return TRUE otherwise FALSE and  Then it checks whether the first element  AND the second element are TRUE if yes, then it will return TRUE otherwise FALSE. It will go on till the last logical operator in a vector and return the results.

Syntax: cumall(logical_vector)

where, logical_vector is the input logical vector.

Example: 

In this example, we are creating a logical vector with 10 elements and apply cumall() function.

R




# load the dplyr library
library("dplyr")
  
# create logical vector
log_vector=c(TRUE, FALSE, FALSE, FALSE,
             FALSE, FALSE, TRUE, TRUE
             FALSE, FALSE)
  
# display
print(log_vector)
  
# apply cumall()
print(cumall(log_vector))


Output:

[1]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE

[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

cumany()

This function will check whether the first element is TRUE. if yes, then it will return TRUE otherwise FALSE and  Then it checks whether the first element  AND  the second element are TRUE if anyone is TRUE, then it will return TRUE otherwise FALSE. It will go on till the last logical operator in a vector and return the results.

Syntax: cumany(logical_vector)

where, logical_vector is the input logical vector.

Example:

In this example, we are creating a logical vector with 10 elements and apply cumany() function.

R




# load the dplyr library
library("dplyr")
  
# create logical vector
log_vector = (TRUE, FALSE, FALSE, FALSE,
              FALSE, FALSE, TRUE, TRUE,
              FALSE, FALSE)
  
# display
print(log_vector)
  
# apply cumany()
print(cumany(log_vector))


Output:

[1]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

cummean()

This function is applied to the numeric vector which will return the cumulative average or mean values for all the elements in a vector by analyzing two cumulative elements.

Syntax: cummean(numeric_vector)

where, numeric_vector is the input numeric vector.

Example: 

In this example, we are creating a numeric vector with 10 elements and apply cummean() function.

R




# load the dplyr library
library("dplyr")
  
# create  vector
num_vector=c(1,23,45,6,4,7,8,9,0,6)
  
# display
print(num_vector)
  
# apply cummean()
print(cummean(num_vector))


Output:

 [1]  1 23 45  6  4  7  8  9  0  6

 [1]  1.00000 12.00000 23.00000 18.75000 15.80000 14.33333 13.42857 12.87500

 [9] 11.44444 10.90000



Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads