How to useslice_max() function in R Language
This function returns the maximum n rows of the dataframe based on a column
Syntax:
dataframe %>% slice_max(column, n )
Where dataframe is the input dataframe, the column is the dataframe column where max rows are returned based on this column and n is the number of maximum rows to be returned
Example: R program to filter using slice_max() function
R
# load the package library (dplyr) # create the dataframe with three columns # id , department and salary with 8 rows data= data.frame (id= c (7058,7059,7060,7089,7072,7078,7093,7034), department= c ( 'IT' , 'sales' , 'finance' , 'IT' , 'finance' , 'sales' , 'HR' , 'HR' ), salary= c (34500.00,560890.78,67000.78,25000.00, 78900.00,25000.00,45000.00,90000)) # display actual dataframe print (data) print ( "==========================" ) # return top 3 maximum rows based on salary # column in the dataframe print (data %>% slice_max (salary, n = 3)) print ( "==========================" ) # return top 5 maximum rows based on department # column in the dataframe print (data %>% slice_max (department, n = 5)) print ( "==========================" ) |
Output:
Filter or subsetting rows in R using Dplyr
In this article, we are going to filter the rows from dataframe in R programming language using Dplyr package.
Dataframe in use: