cumall(), cumany() & cummean() R Functions of dplyr Package
Last Updated :
02 Jun, 2022
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
library ( "dplyr" )
log_vector= c ( TRUE , FALSE , FALSE , FALSE ,
FALSE , FALSE , TRUE , TRUE ,
FALSE , FALSE )
print (log_vector)
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
library ( "dplyr" )
log_vector = ( TRUE , FALSE , FALSE , FALSE ,
FALSE , FALSE , TRUE , TRUE ,
FALSE , FALSE )
print (log_vector)
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
library ( "dplyr" )
num_vector= c (1,23,45,6,4,7,8,9,0,6)
print (num_vector)
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
Share your thoughts in the comments
Please Login to comment...