Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Windows Function in R using Dplyr
Next article icon

Windows Function in R using Dplyr

Last Updated : 10 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Aggregation functions in R are used to take a bunch of values and give us output as a single value. Some of the examples of aggregation methods are the sum and mean. Windows functions in R provide a variation to the aggregation methods in the sense that they return the number of outputs equivalent to the number of inputs if n number of inputs are taken, n outputs are returned. In this article, we will discuss the various windows functions that are available in R.

The functions we will covering in this articles are : 

row_numberTo rank the values.
min_rankTo compute the rank so that the minimum rank until that element is thrown as output.
percent_rankTo compute the rank so that the percentage rank between the values 0 and 1 is returned. 
cume_distTo compute a proportion of all values at most equal to the current rank.
LeadTo compute the next element in sequence of values specified in the vector. 
LagTo compute the previous element in sequence of values specified in the vector. 
Cum Sum MethodTo compute the sum of values encountered till that particular index. 
Cum Prod MethodTo compute the product of values encountered till that particular index. 
Cum Min MethodTo calculate the minimum value encountered until that particular index value. 
Cum Max MethodTo calculate the maximum value encountered until that particular index value.
Cum Mean MethodTo calculate the mean value encountered until that particular index value.
Cum Any MethodTo check if any of the elements in the vector satisfy the result. 
Cum All MethodTo check if all of the elements in the vector satisfy the result.

Let's see the syntax and Code for each function.

Row_number 

The row_number method is considered to be equivalent to the rank method. The missing values are left as it is.

Syntax: row_number(vec)

Arguments:  vec- the vector of values that have to be ranked

R
library(dplyr) library(data.table) #creating a data vector companies =  c("Geekster","Geeksforgeeks","Wipro","TCS") #printing the original vector print(companies) #computing the row number of the used vector rn <- row_number(companies) print(rn) 

Output:

"Geekster" "Geeksforgeeks" "Wipro" "TCS"           2 1 4 3

Explanation:

The row numbers of the supplied input vector are computed after sorting the values in increasing order. For instance, the word(GeeksForGeeks) in the first index is the smallest lexicographically. Therefore its row number is 1. This is followed by the word "Geekster" with the row number corresponding to 2. TCS gets row number 3 since it is next in order.

Min_rank

The min_rank method is also used to compute the rank in such a way that the minimum rank until that element is thrown as output.

Syntax: min_rank(vec)

Arguments: vec- the vector of values that have to be ranked

R
#computing the rank of the used vector companies =  c("Geekster","Geeksforgeeks",                "Geekster","Wipro","TCS") min_rank <- min_rank(companies) print(min_rank) 

Output: 

2 1 2 5 4

Here we can see that Geekster has the min_rank of 2 so it assigned the same values i.e. not 3

Percent_rank 

The percent_rank method is also used to compute the rank in such a way that the percentage rank between the values 0 and 1 is returned. 

Syntax: percent_rank(vec)

Arguments: vec- the vector of values that have to be ranked

R
#computing the rank of the used vector percent_rank <- percent_rank(companies) print(percent_rank) 

Output:

0.25 0.00 0.25 1.00 0.75

The values begin with the 0.0 percentage after being sorted in ascending order. 

Cume_dist 

The cume_dist method in R is equivalent to a cumulative distribution function. It is used to compute a proportion of all values at most equal to the current rank.

Syntax: cume_dist(vec)

Arguments: vec- the vector of values that have to be ranked

R
#computing the cume_dist of the used vector dist <- cume_dist(companies) print(dist) 

Output:

0.6 0.2 0.6 1.0 0.8

Lead

The lead windows method in R is by default used to compute the next element in sequence of values specified in the vector. The lead value is not applicable for the last element of the input data object. 

Syntax: lead(vec)

Arguments: vec- the vector of values that have to be ranked.

R
#creating a vector  vec <- c(4,3,1,2,5) print(vec)  lead <- lead(vec) print(lead) 

Output:

4 3 1 2 5 3 1 2 5 NA

Lag

The lag windows method in R is by default used to compute the previous element in sequence of values specified in the vector. The lag value is not applicable for the first element of the input data object, since there is no element before it.  

Syntax: lag(vec)

Arguments: vec- the vector of values that have to be ranked

R
lag <- lag(vec) print(lag) 

Output:

NA  4  3  1  2

Explanation : 

The lag method for the first element is not applicable. For the element at 1st index that is 3 , the lag value is equivalent to the value at the 0th index. 

Cum Sum Method

The cumsum() method is used to compute the sum of values encountered till that particular index. The cumsum value of the first element is equivalent to the value itself. 

Syntax: cumsum(vec)

Arguments: vec- the vector of values that have to be ranked

R
#creating a vector  vec <- 1:5 cumsum <- cumsum(vec) print(cumsum) 

Output:

1  3  6 10 15

Explanation : 

The sum of the first index element, 2 in the vector is 1+2 = 3. For the element 3 at index 2 in vector, cumsum = 1 + 2 + 3 = 6. Similarly the cumulative sums can be calculated. 

Cum Prod Method

The cumprod() method is used to compute the product of values encountered till that particular index. The cumprod value of the first element is equivalent to the value itself.

Syntax: cumprod(vec)

Arguments: vec- the vector of values

R
cumprod <- cumprod(vec) print(cumprod) 

Output:

1 2 6 24 120

Explanation :

The product of 0th index element is the value itself, equivalent to 1.The product of the first index element, 2 in the vector is 122 = 3. For the element 3 at index 2 in vector, cumprod = 1 * 2 * 3 = 6. Similarly the cumulative products can be calculated. 

Cum Min Method

The cummin() method is used to calculate the minimum value encountered until that particular index value. 

Syntax: cummin(vec)

Arguments: vec- the vector of values

R
#creating a vector  vec <- c(3,2,1,5,3) cum_min <- cummin(vec) print(cum_min) 

Output:

3 2 1 1 1

Explanation : 

The min value encountered till first element is the element value itself. In the second element 2, the minimum becomes 2. For the third element, min becomes 1. The fourth element is greater than min value therefore, min remains same. 

Cum Max Method

The cummax() method is used to calculate the maximum value encountered until that particular index value.

Syntax: cummax(vec)

Arguments: vec- the vector of values

R
cum_max <- cummax(vec) print(cum_max) 

Output:

3 3 3 5 5

Cum Mean Method

The cummean() method is used to calculate the mean value encountered until that particular index value.

Syntax: cummean(vec)

Arguments: vec- the vector of values

R
cum_mean <- cummean(vec) print(cum_mean) 

Output:

3.00 2.50 2.00 2.75 2.80

Cum Any Method

The cumany method is used to check if any of the elements in the vector satisfy the result. The elements of the vector at any particular index are taken in account to consider the function value. 

Syntax: cumany(vec)

Arguments: vec- the vector of values

R
cum_any_3 <- cumany(vec>3) print("Any vector values greater than 3") print(cum_any_3)  cum_any_0 <- cumany(vec==0) print("Any vector values equal to 0") print(cum_any_0) 

Output:

"Any vector values greater than 3" FALSE FALSE FALSE  TRUE  TRUE "Any vector values equal to 0" FALSE FALSE FALSE FALSE FALSE

Cum All Method

The cumall method is used to check if all of the elements in the vector satisfy the result. The elements of the vector at any particular index are taken in account to consider the function value. 

Syntax: cumall(vec)

Arguments: vec- the vector of values

R
#using cumall method cum_any_3 <- cumall(vec>3) print(cum_any_3) 

Output:

FALSE FALSE FALSE FALSE FALSE

Next Article
Windows Function in R using Dplyr

Y

yashchuahan
Improve
Article Tags :
  • R Language

Similar Reads

    Apply a function to each group using Dplyr in R
    In this article, we are going to learn how to apply a function to each group using dplyr in the R programming language. The dplyr package in R is used for data manipulations and modifications. The package can be downloaded and installed into the working space using the following command :  install.p
    4 min read
    Slice() From Dplyr In R
    With so much data around us in today's world, dealing with them becomes tough. In this case, the Dplyr data frame package from R acts as a lifesaver and that package stands out as a powerful and versatile tool. for data manipulation. In R Programming Language package has many functions and among the
    11 min read
    dcast() Function in R
    Reshaping data in R Programming Language is the process of transforming the structure of a dataset from one format to another. This transformation is done by the dcast function in R. dcast function in RThe dcast() function in R is a part of the reshape2 package and is used for reshaping data from 'l
    5 min read
    What Is The Data() Function In R?
    In this article, we will discuss what is data function and how it works in R Programming Language and also see all the available datasets in R. Data() Function In RIn R, the data() function is used to load datasets that come pre-installed with R packages or datasets that have been explicitly install
    5 min read
    How to Use file.path() Function in R
    R programming language is becoming popular among developers, analysts, and mainly for data scientists. Students are eagerly learning R with Python language to use their analytical skills at their best. While learning any language, one is faced with many difficulties, and the individual learning R Pr
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences